Apache Http Client 4 上传多个文件 (示例代码可在 github 上找到)
2015-07-01 10:15
791 查看
转自:http://www.baeldung.com/httpclient-multipart-upload
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.
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
?
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.
Example 3.1. – Uploading Text and a Text File Part
?
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
?
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
?
Notice the ContentType – which is now specifying binary data.
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.
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 toadd 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
?
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
?
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
?
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
?
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.
相关文章推荐
- HTTP协议是无状态协议,怎么理解? session cookie
- 搜索广告与广告网络Demand技术-流式计算平台
- 用Netty实现的简单HTTP服务器
- Android通过HTTP协议实现上传文件数据
- Asynchronous HTTP Requests in Android Using Volley
- Webrtc Intro - RTP RTCP SDP relevants
- [管理篇]VMWare搭建Openstack——将FlatDHCP网络转化为GRE网络——设置浮动IP
- post 相比get 有很多优点,为什么现在的HTTP通信中大多数请求还是使用get?
- 一个int数组,将数组中重复次数超过k次的元素变成0,并将变成0的元素放到数组最后http://bbs.csdn.net/topics/390921701?page=1#post-398464797#
- gRPC:Google开源的基于HTTP/2和ProtoBuf的通用RPC框架
- Android 应用锁/程序锁(APPLock)的实现 http://shigongbo.blog.163.com/blog/static/976090201452572535715
- http1.0与http1.1的区别
- heartbeatV1+nfs实现高可用httpd
- Linux网络编程综合运用之MiniFtp实现(九)
- Linux内核软RPS实现网络接收软中断的负载均衡分发
- Linux内核软RPS实现网络接收软中断的负载均衡分发
- [NIO]用dawn发送接收HTTP请求
- 分析并实现 360 P1路由器上的朋友专享网络 功能
- iOS网络层设计方案
- LVS+Keepalived+httpd安装及配置