Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package mobi.salesforce.client.upload; import java.io.File; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; /** * * @author Juthakiat Tipchai <juthakiat@gmail.com> */ public class DemoFileUploader { public static void main(String args[]) throws Exception { DemoFileUploader fileUpload = new DemoFileUploader(); File file = new File("/Users/Kong/Desktop/a.jpg"); // Upload the file fileUpload.executeMultiPartRequest("http://localhost:8080/m2salesforce/resources/file/upload", file, file.getName(), "File Uploaded :: a.jpg"); } public void executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(urlString); try { // Set various attributes MultipartEntity multiPartEntity = new MultipartEntity(); multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : "")); multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())); FileBody fileBody = new FileBody(file, "application/octect-stream"); // Prepare payload multiPartEntity.addPart("attachment", fileBody); // Set to request body postRequest.setEntity(multiPartEntity); // Send request HttpResponse response = client.execute(postRequest); // Verify response if any if (response != null) { System.out.println(response.getStatusLine().getStatusCode()); } } catch (Exception ex) { } } }