Go to file upload using HttpClient 4

Source: Internet
Author: User

Http://www.tuicool.com/articles/Y7reYb

1. Overview

In this tutorial we will describe how to use HttpClient 4 for a multi-file upload operation .

We will use http://echo.200please.com as the test server because it is public-facing and accepts most types of content.

If you want to learn more and learn about other great things you can do with HttpClient -then take a look at the top HttpClient tutorials.

2. Using the Addpart method

Let's start by studying the Multipartentitybuilder object to add components to an HTTP entity that will be uploaded later via a post operation.

This is the general way to add ingredients to a httpentity to represent a form .

Example 2.1. -Upload a form using two text ingredients and one 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 also instantiate the file object by setting the ContentType value that will be used by the server.

Also note that the Addpart method has two parameters that act as a key-value pair for the form. Unless the server side actually needs these values and uses these parameter names, they are involved, otherwise they are simply ignored.

3. Using the addbinarybody and addtextbody methods

The more straightforward way to create a multipart entity is to use the addbinarybody and addtextbody methods. These methods serve to upload text, files, character arrays, and inputstream objects. We used a simple example to describe how to use them.

Example 3.1. -Upload a text and a text file section

New HttpPost ("This isa 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 ();p ost.setentity (entity); HttpResponse response = Client.execute (post);   

Note that filebody and stringbody objects are not required here

It is also important that most servers do not check the ContentType of the text body, so the addtextbody method may ignore the ContentType value.

Addbinarybody's The API accepts a ContentType -but it is also possible to create an entity from a binary body, while the corresponding name of the form parameter holds the file. As described in the previous section, if the contenttype value is not specified, some servers will not recognize the file.

Next, we add a zip file as a InputStream, and the picture file will be added as file object:

Example 3.2. -Upload a zip file, a picture file and a text block

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 ();p ost.setentity (entity); HttpResponse response = Client.execute (post);           

Note that the ContentType value can be created dynamically, as shown in the example above for a zip file.

Finally, not all servers accept the inputstream section. We can accept this in the first line of the code that is manifested by the server.

Let's take a look at another example, addbinarybody is used directly for a bit array :

Example 3.3. -Upload a bit array and text

New HttpPost ("This isa multipart post";  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 ();p ost.setentity (entity); HttpResponse response = Client.execute (post);    

Note ContentType -It is now specified as binary data.

4. Summary

This document presents Multipartentitybuilder as a flexible object that provides a variety of APIs for creating a multipart form.

The example also shows how to use HttpClient to upload a httpentity similar to a form entity.

All of the implementations and code blocks for these examples can be found in my GitHub project – This is an eclipse-based project, so it's easy to import and run.

Go to file upload using HttpClient 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.