微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – HTTPClient 4.1中包含文件和字符串的多部分POST

我需要创建包含字段的多部分POST请求:
update [image_title] =字符串
更新[image] =图像数据本身.
正如你所看到的,两者都在称为“更新”的关联数组中.
我怎么能用HTTPClient 4.1来做,因为我只发现了这个库的3.x行的例子.

先感谢您.

解决方法

可能为时已晚,但可能对某人有帮助.我有完全相同的问题.
假设您有一个文件对象,其中包含有关图像的必要信息
HttpPost post = new HttpPost(YOUR_URL);
multipartentity entity = new multipartentity();
ByteArrayBody body = new ByteArrayBody(file.getData(),file.getName());     
String imageTitle = new StringBody(file.getName());

entity.addPart("imageTitle",imageTitle);
entity.addPart("image",body);
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
    try {
        response = client.execute(post);
    } catch (ClientProtocolException e) {
        e.printstacktrace();
    } catch (IOException e) {
        e.printstacktrace();
    }

请注意,multipartentity是HttpMime模块的一部分.因此,您需要将该jar放在lib目录中或包含为(maven / gradle)依赖项.

原文地址:https://www.jb51.cc/java/128023.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐