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. SummaryThis 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. |