Multipart Upload with HttpClient 4--reference
by Eugen Paraschiv on May 23, 2014 in HttpClient
http://www.baeldung.com/httpclient-multipart-upload
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 to the main HttpClient tutorial.
2. Using the AddPart Method
Let’s start by looking at the MultipartEntityBuilder object to add parts to a Http entitywhich 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.
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, and InputStream objects. Lets illustrate how with simple examples.
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 also possible 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 the ContentTypevalue is not specified.
Next, we’ll add a zip file as an InputStream, while the image file will be added as Fileobject:
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.
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.
相关文章
- shell脚本--数值比较
- 【AIX 学习】性能优化--topas
- Java泛型--泛型应用--泛型接口、泛型方法、泛型数组、泛型嵌套
- Java面向对象基础--内部类
- 第一百六十一节,封装库--JavaScript,完整封装库文件
- [Git] Recover Local Changes from `git reset --hard` with `git reflog`
- [Django] Get started with Django -- Install python and virtualenv
- [React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD
- 小白学数据分析--付费渗透率再研究
- [Javascript] Broadcaster + Operator + Listener pattern -- 13. Repeat When Done with a Repeat Operator
- [Intro to Deep Learning with PyTorch -- L2 -- N24] Logistic Regression Algorithm
- [Intro to Deep Learning with PyTorch -- L2 -- N14] Sigmoid function
- [Functional Programming] Working with two functors(Applicative Functors)-- Part1 --.ap
- [AngularFire2] Auth with Firebase auth -- email
- No space left on device you must specify the filesystem type--Linux重启挂在失败
- 设计模式学习笔记--备忘录(Mamento)模式
- 情绪管理--不要总做“好脾气”的人。
- Hadoop-2.2.0中国文献—— MapReduce 下一代 -- 公平调度
- Kotlin的报错提示:Invoke-customs are only supported starting with Android O (--min-api 26)