Using java Syntax Highlighting
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.entity.mime.content.StringBody;
- import java.io.File;
- import java.io.UnsupportedEncodingException;
- ...
- MultipartEntity multipart = new MultipartEntity();
- File file = new File("/filepath"); // File with some location (filepath)
- Charset chars = Charset.forName("UTF-8"); // Setting up the encoding
- FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file
- multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file
- StringBody stringB; // Now lets add some extra information in a StringBody
- try {
- stringB = new StringBody("I am the caption of the file",chars); // Adding the content to the StringBody and setting up the encoding
- multipart.addPart("caption", stringB); // Add the part to my MultipartEntity
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- HttpPost post = new HttpPost(url); // Setting up a HTTP Post method with the target url
- post.setEntity(multiPartEntity); // Setting the multipart Entity to the post method
- HttpResponse resp = client.execute(post); // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
In order to use this snippet you have to import the following libraries:
http://james.apache.org/mime4j/index.html - Mime4j
http://hc.apache.org/httpcomponents-cli ... index.html - HttpMime
http://commons.apache.org/io/ - Commons IO
SDK Version compatible: 0.9, 1.0



