Apache Http Client 4 上传多个文件 (示例代码可在 github 上找到)
来源:互联网 发布:淘宝女式薄绒内衣套装 编辑:程序博客网 时间:2024/06/08 22:39
转自:http://www.baeldung.com/httpclient-multipart-upload
Multipart Upload with HttpClient 4
1. Overview
In this tutorial we will illustrate how to do a multipart upload operation using HttpClient 4.
We’ll use http://echo.200please.com as a test server because it’s public and it accepts most types of content.
If you want to dig deeper and learn other cool things you can do with the HttpClient – head on over tothe main HttpClient tutorial.
2. Using the AddPart Method
Let’s start by looking at the MultipartEntityBuilder object to add parts to a Http entity which will then be uploaded via a POST operation.
This is a generic method to add parts to an HttpEntity representing the form.
Example 2.1. – Uploading a Form with Two Text Parts and a File
File file =
new
File(textFileName, ContentType.DEFAULT_BINARY);
HttpPost post =
new
HttpPost(
"http://echo.200please.com"
);
FileBody fileBody =
new
FileBody(file);
StringBody stringBody1 =
new
StringBody(
"Message 1"
, ContentType.MULTIPART_FORM_DATA);
StringBody stringBody2 =
new
StringBody(
"Message 2"
, ContentType.MULTIPART_FORM_DATA);
//
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart(
"upfile"
, fileBody);
builder.addPart(
"text1"
, stringBody1);
builder.addPart(
"text2"
, stringBody2);
HttpEntity entity = builder.build();
//
post.setEntity(entity);
HttpResponse response = client.execute(post);
Note that we’re instantiating the File object by also specifying the ContentType value to be used by the server.
Also, note that the addPart method has two arguments, acting like key/value pairs for the form. These are only relevant if the server side actually expects and uses parameter names – otherwise they’re simply ignored.
3. Using the addBinaryBody and addTextBody Methods
A more direct way to create a multipart entity is to use the addBinaryBody andAddTextBody methods. These methods work for uploading text, files, character arrays, andInputStream objects. Lets illustrate how with simple examples.
Example 3.1. – Uploading Text and a Text File Part
HttpPost post =
new
HttpPost(
"http://echo.200please.com"
);
File file =
new
File(textFileName);
String message =
"This is a multipart post"
;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody(
"upfile"
, file, ContentType.DEFAULT_BINARY, textFileName);
builder.addTextBody(
"text"
, message, ContentType.DEFAULT_BINARY);
//
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
Note that the FileBody and StringBody objects are not needed here.
Also important, most servers do not check the ContentType of the text body, so theaddTextBody method may omit the ContentType value.
The addBinaryBody API accepts a ContentType – but it is alsopossible to create the entity just from a binary body and the name of the form parameter holding the file. As stated in the previous section some servers will not recognize the file if theContentType value is not specified.
Next, we’ll add a zip file as an InputStream, while the image file will be added asFile object:
Example 3.2. – Uploading a Zip File, an Image File and a Text Part
HttpPost post =
new
HttpPost(
"http://echo.200please.com"
);
InputStream inputStream =
new
FileInputStream(zipFileName);
File file =
new
File(imageFileName);
String message =
"This is a multipart post"
;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody
(
"upfile"
, file, ContentType.DEFAULT_BINARY, imageFileName);
builder.addBinaryBody
(
"upstream"
, inputStream, ContentType.create(
"application/zip"
), zipFileName);
builder.addTextBody(
"text"
, message, ContentType.TEXT_PLAIN);
//
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
Note that the ContentType value can be created on the fly, as is the case in the example above for the zip file.
Finally, not all servers acknowledge InputStream parts. The server we instantiated in the first line of the code recognizes InputStreams.
Let’s now look at another example where addBinaryBody is working directly with a byte array :
Example 3.3. – Uploading a Byte Array and Text
HttpPost post =
new
HttpPost(
"http://echo.200please.com"
);
String message =
"This is a multipart post"
;
byte
[] bytes =
"binary code"
.getBytes();
//
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody(
"upfile"
, bytes, ContentType.DEFAULT_BINARY, textFileName);
builder.addTextBody(
"text"
, message, ContentType.TEXT_PLAIN);
//
HttpEntity entity = builder.build();
post.setEntity(entity);
HttpResponse response = client.execute(post);
Notice the ContentType – which is now specifying binary data.
4. Conclusion
This article has presented the MultipartEntityBuilder as a flexible object which offer multiple API choices to create a multipart form.
The examples have also shown how to use the HttpClient to upload a HttpEntity that similar to a form entity.
The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.
- Apache Http Client 4 上传多个文件 (示例代码可在 github 上找到)
- apache http client 上传文件
- struts2示例代码-多个文件上传
- 在macbook上使用github上传代码
- 如何在sharepoint上上传和下载多个文件
- 如何在mac电脑上传代码到github上
- 如何在 Mac 上的 IDEA 上传代码到 GitHub
- 上传文件系统,可添加多个文件
- 上传代码到GitHub上
- Apache服务器的vhost------可实现在同一台服务器上放置多个网站
- github 本地代码上传到github上
- 利用socket模拟http的混合表单上传(在一个请求中提交表单并上传多个文件)
- http上传文件代码
- 使用HTTP Client 上传文件到服务器
- (网摘)Flex中关于多个文件上传的示例代码
- 可以在CR t0.c文件中找到这4个启动函数的代码
- NodeJS文件上传示例代码
- http client 4可运行的程序
- UNIX/LINUX使用expect实现人机自动交互功能
- java.util.zip类 ZipInputStream
- [华为机试练习题]25.圆桌游戏
- PHP+MySQL分页原理实现
- 如何当一个android面试官
- Apache Http Client 4 上传多个文件 (示例代码可在 github 上找到)
- 杭电 HDU ACM 1754 I Hate It (线段树)
- 国际化困境(第二篇)
- AutoLayout中的Visual Format Language
- 程序员技术练级攻略
- 测试面试突击——阅读笔记
- 配置Spring数据源
- java.util.zip类 ZipFile
- 【九度】抓取九度AC所有代码以及在CSDN的解题思路链接形成文件提交至github