List of usage examples for org.apache.http.entity.mime MultipartEntity addPart
public void addPart(final String name, final ContentBody contentBody)
From source file:com.woonoz.proxy.servlet.HttpEntityEnclosingRequestHandler.java
private HttpEntity createMultipartEntity(HttpServletRequest request) throws FileUploadException, IOException { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); MultipartEntity multipartEntity = new MultipartEntity(); FileItemIterator iterator = servletFileUpload.getItemIterator(request); while (iterator.hasNext()) { FileItemStream fileItem = iterator.next(); final String partName = fileItem.getFieldName(); if (fileItem.isFormField()) { multipartEntity.addPart(partName, buildStringBody(fileItem)); } else {//from w w w .j av a2 s . c om multipartEntity.addPart(partName, buildContentBodyFromFileItem(fileItem)); } } return multipartEntity; }
From source file:com.woonoz.proxy.servlet.HttpPostRequestHandler.java
private HttpEntity createMultipartEntity(HttpServletRequest request, HttpPost httpPost) throws FileUploadException, IOException { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); MultipartEntity multipartEntity = new MultipartEntity(); FileItemIterator iterator = servletFileUpload.getItemIterator(request); while (iterator.hasNext()) { FileItemStream fileItem = iterator.next(); final String partName = fileItem.getFieldName(); if (fileItem.isFormField()) { multipartEntity.addPart(partName, buildStringBody(fileItem)); } else {//from www.j a v a 2 s . co m multipartEntity.addPart(partName, buildContentBodyFromFileItem(fileItem)); } } return multipartEntity; }
From source file:ecblast.test.EcblastTest.java
public String executeCompareReactions(File queryFile, File targetFile) { String urlString = "http://localhost:8080/ecblast-rest/compare"; DefaultHttpClient client;//from w w w. j av a 2s . c o m client = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(urlString); try { //Set various attributes MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); FileBody queryFileBody = new FileBody(queryFile); //Prepare payload multiPartEntity.addPart("q", queryFileBody); multiPartEntity.addPart("Q", new StringBody("RXN", "text/plain", Charset.forName("UTF-8"))); FileBody targetFileBody = new FileBody(targetFile); multiPartEntity.addPart("t", targetFileBody); multiPartEntity.addPart("T", new StringBody("RXN", "text/plain", Charset.forName("UTF-8"))); //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()); return response.toString(); } } catch (IOException ex) { return null; } return null; }
From source file:at.univie.sensorium.extinterfaces.HTTPSUploader.java
private String uploadFiles(List<File> files) { String result = ""; try {//w w w . ja v a 2 s . co m if (URLUtil.isValidUrl(posturl)) { HttpClient httpclient = getNewHttpClient(); HttpPost httppost = new HttpPost(posturl); MultipartEntity mpEntity = new MultipartEntity(); // MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); mpEntity.addPart("username", new StringBody(username)); mpEntity.addPart("password", new StringBody(password)); for (File file : files) { Log.d(SensorRegistry.TAG, "preparing " + file.getName() + " for upload"); ContentBody cbFile = new FileBody(file, "application/json"); mpEntity.addPart(file.toString(), cbFile); } httppost.addHeader("username", username); httppost.addHeader("password", password); httppost.setEntity(mpEntity); HttpResponse response = httpclient.execute(httppost); String reply; InputStream in = response.getEntity().getContent(); StringBuilder sb = new StringBuilder(); try { int chr; while ((chr = in.read()) != -1) { sb.append((char) chr); } reply = sb.toString(); } finally { in.close(); } result = response.getStatusLine().toString(); Log.d(SensorRegistry.TAG, "Http upload completed with response: " + result + " " + reply); } else { result = "URL invalid"; Log.d(SensorRegistry.TAG, "Invalid http upload url, aborting."); } } catch (IllegalArgumentException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (FileNotFoundException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (ClientProtocolException e) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } catch (IOException e) { result = "upload failed due to timeout"; StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Log.d(SensorRegistry.TAG, sw.toString()); } return result; }
From source file:fi.hut.soberit.sensors.services.BatchDataUploadService.java
protected void populateMultipartEntity(MultipartEntity multipartEntity, File file) { multipartEntity.addPart(uploadFileField, new FileBody(file)); }
From source file:com.cottsoft.weedfs.client.WeedfsClient.java
/** * Description<br>//from ww w. ja v a2s . co m * Cache local file to WeedFS Server * * @version v1.0.0 * @param file * @return */ public RequestResult cache(File file) { RequestResult result = null; Gson gson = new Gson(); if (!file.exists()) { throw new IllegalArgumentException("File doesn't exist"); } // HTTP REQUEST begin result = new RequestResult(); WeedAssign assignedInfo = null; BufferedReader in = null; // 1. Send assign request and get fid try { StringBuffer host = new StringBuffer(); host.append("http://"); host.append(this.masterHost); host.append(":"); host.append(this.masterPort); host.append("/"); //HttpUtil.request("http://" + this.masterHost + ":" + this.masterPort+ "/", "dir/assign", "GET") in = new BufferedReader( new InputStreamReader(HttpUtil.request(host.toString(), assign, EHttpMethod.GET))); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } // format HTTP Response to Assigned Info. assignedInfo = gson.fromJson(response.toString(), WeedAssign.class); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } finally { try { // close input stream. if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } // 2. Send cache file request on volume server FileBody fileBody = new FileBody(file, "text/plain"); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); StringBuffer uri = new StringBuffer(); uri.append("http://"); uri.append(assignedInfo.getPublicUrl()); uri.append("/"); uri.append(assignedInfo.getFid()); HttpPost post = new HttpPost(uri.toString()); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("fileBody", fileBody); post.setEntity(entity); try { // Add File char. String response = EntityUtils.toString(client.execute(post).getEntity(), "UTF-8"); client.getConnectionManager().shutdown(); FileResult fileResult = gson.fromJson(response, FileResult.class); result.setFid(assignedInfo.getFid()); result.setSize(fileResult.getSize()); result.setStatus(true); result.setFileUrl(uri.toString()); return result; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.toString()); } }
From source file:org.wso2.am.integration.tests.publisher.APIM614AddDocumentationToAnAPIWithDocTypeSampleAndSDKThroughPublisherRestAPITestCase.java
@Test(groups = { "wso2.am" }, description = "Add Documentation To An API With Type Sample SDK And" + " Source File through the publisher rest API ", dependsOnMethods = "testAddDocumentToAnAPIHowToFile") public void testAddDocumentToAnAPISDKToFile() throws Exception { String fileNameAPIM622 = "APIM622.txt"; String docName = "APIM622PublisherTestHowTo-File-summary"; String docType = "samples"; String sourceType = "file"; String summary = "Testing"; String mimeType = "text/plain"; String docUrl = "http://"; String filePathAPIM622 = TestConfigurationProvider.getResourceLocation() + File.separator + "artifacts" + File.separator + "AM" + File.separator + "lifecycletest" + File.separator + fileNameAPIM622; String addDocUrl = publisherUrls.getWebAppURLHttp() + "publisher/site/blocks/documentation/ajax/docs.jag"; //Send Http Post request to add a new file HttpPost httppost = new HttpPost(addDocUrl); File file = new File(filePathAPIM622); FileBody fileBody = new FileBody(file, "text/plain"); //Create multipart entity to upload file as multipart file MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("docLocation", fileBody); multipartEntity.addPart("mode", new StringBody("")); multipartEntity.addPart("docName", new StringBody(docName)); multipartEntity.addPart("docUrl", new StringBody(docUrl)); multipartEntity.addPart("sourceType", new StringBody(sourceType)); multipartEntity.addPart("summary", new StringBody(summary)); multipartEntity.addPart("docType", new StringBody(docType)); multipartEntity.addPart("version", new StringBody(apiVersion)); multipartEntity.addPart("apiName", new StringBody(apiName)); multipartEntity.addPart("action", new StringBody("addDocumentation")); multipartEntity.addPart("provider", new StringBody(apiProvider)); multipartEntity.addPart("mimeType", new StringBody(mimeType)); multipartEntity.addPart("optionsRadios", new StringBody(docType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); httppost.setEntity(multipartEntity); //Upload created file and validate HttpResponse response = httpClient.execute(httppost); HttpEntity entity = response.getEntity(); JSONObject jsonObject1 = new JSONObject(EntityUtils.toString(entity)); assertFalse(jsonObject1.getBoolean("error"), "Error when adding files to the API "); }
From source file:org.wso2.am.integration.tests.publisher.APIM614AddDocumentationToAnAPIWithDocTypeSampleAndSDKThroughPublisherRestAPITestCase.java
@Test(groups = { "wso2.am" }, description = "Add Documentation To An API With Type public forum And" + " Source File through the publisher rest API ", dependsOnMethods = "testAddDocumentToAnAPISDKToFile") public void testAddDocumentToAnAPIPublicToFile() throws Exception { String fileNameAPIM624 = "APIM624.txt"; String docName = "APIM624PublisherTestHowTo-File-summary"; String docType = "public forum"; String sourceType = "file"; String summary = "Testing"; String mimeType = "text/plain"; String docUrl = "http://"; String filePathAPIM624 = TestConfigurationProvider.getResourceLocation() + File.separator + "artifacts" + File.separator + "AM" + File.separator + "lifecycletest" + File.separator + fileNameAPIM624; String addDocUrl = publisherUrls.getWebAppURLHttp() + "publisher/site/blocks/documentation/ajax/docs.jag"; //Send Http Post request to add a new file HttpPost httppost = new HttpPost(addDocUrl); File file = new File(filePathAPIM624); FileBody fileBody = new FileBody(file, "text/plain"); //Create multipart entity to upload file as multipart file MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("docLocation", fileBody); multipartEntity.addPart("mode", new StringBody("")); multipartEntity.addPart("docName", new StringBody(docName)); multipartEntity.addPart("docUrl", new StringBody(docUrl)); multipartEntity.addPart("sourceType", new StringBody(sourceType)); multipartEntity.addPart("summary", new StringBody(summary)); multipartEntity.addPart("docType", new StringBody(docType)); multipartEntity.addPart("version", new StringBody(apiVersion)); multipartEntity.addPart("apiName", new StringBody(apiName)); multipartEntity.addPart("action", new StringBody("addDocumentation")); multipartEntity.addPart("provider", new StringBody(apiProvider)); multipartEntity.addPart("mimeType", new StringBody(mimeType)); multipartEntity.addPart("optionsRadios", new StringBody(docType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); httppost.setEntity(multipartEntity); //Upload created file and validate HttpResponse response = httpClient.execute(httppost); HttpEntity entity = response.getEntity(); JSONObject jsonObject1 = new JSONObject(EntityUtils.toString(entity)); assertFalse(jsonObject1.getBoolean("error"), "Error when adding files to the API "); }
From source file:org.wso2.am.integration.tests.publisher.APIM614AddDocumentationToAnAPIWithDocTypeSampleAndSDKThroughPublisherRestAPITestCase.java
@Test(groups = { "wso2.am" }, description = "Add Documentation To An API With Type support forum And" + " Source File through the publisher rest API ", dependsOnMethods = "testAddDocumentToAnAPIPublicToFile") public void testAddDocumentToAnAPISupportToFile() throws Exception { String fileNameAPIM626 = "APIM626.txt"; String docName = "APIM626PublisherTestHowTo-File-summary"; String docType = "support forum"; String sourceType = "file"; String summary = "Testing"; String mimeType = "text/plain"; String docUrl = "http://"; String filePathAPIM626 = TestConfigurationProvider.getResourceLocation() + File.separator + "artifacts" + File.separator + "AM" + File.separator + "lifecycletest" + File.separator + fileNameAPIM626; String addDocUrl = publisherUrls.getWebAppURLHttp() + "publisher/site/blocks/documentation/ajax/docs.jag"; //Send Http Post request to add a new file HttpPost httppost = new HttpPost(addDocUrl); File file = new File(filePathAPIM626); FileBody fileBody = new FileBody(file, "text/plain"); //Create multipart entity to upload file as multipart file MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("docLocation", fileBody); multipartEntity.addPart("mode", new StringBody("")); multipartEntity.addPart("docName", new StringBody(docName)); multipartEntity.addPart("docUrl", new StringBody(docUrl)); multipartEntity.addPart("sourceType", new StringBody(sourceType)); multipartEntity.addPart("summary", new StringBody(summary)); multipartEntity.addPart("docType", new StringBody(docType)); multipartEntity.addPart("version", new StringBody(apiVersion)); multipartEntity.addPart("apiName", new StringBody(apiName)); multipartEntity.addPart("action", new StringBody("addDocumentation")); multipartEntity.addPart("provider", new StringBody(apiProvider)); multipartEntity.addPart("mimeType", new StringBody(mimeType)); multipartEntity.addPart("optionsRadios", new StringBody(docType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); httppost.setEntity(multipartEntity); //Upload created file and validate HttpResponse response = httpClient.execute(httppost); HttpEntity entity = response.getEntity(); JSONObject jsonObject1 = new JSONObject(EntityUtils.toString(entity)); assertFalse(jsonObject1.getBoolean("error"), "Error when adding files to the API "); }
From source file:org.wso2.am.integration.tests.publisher.APIM614AddDocumentationToAnAPIWithDocTypeSampleAndSDKThroughPublisherRestAPITestCase.java
@Test(groups = { "wso2.am" }, description = "Add Documentation To An API With Type HowTo And" + " Source File through the publisher rest API ", dependsOnMethods = "testApiCreation") public void testAddDocumentToAnAPIHowToFile() throws Exception { String fileNameAPIM614 = "APIM614.txt"; String docName = "APIM614PublisherTestHowTo-File-summary"; String docType = "How To"; String sourceType = "file"; String summary = "Testing"; String mimeType = "text/plain"; String docUrl = "http://"; String filePathAPIM614 = TestConfigurationProvider.getResourceLocation() + File.separator + "artifacts" + File.separator + "AM" + File.separator + "lifecycletest" + File.separator + fileNameAPIM614; ;//from w ww .ja va 2 s .c o m String addDocUrl = publisherUrls.getWebAppURLHttp() + "publisher/site/blocks/documentation/ajax/docs.jag"; //Send Http Post request to add a new file HttpPost httppost = new HttpPost(addDocUrl); File file = new File(filePathAPIM614); FileBody fileBody = new FileBody(file, "text/plain"); //Create multipart entity to upload file as multipart file MultipartEntity multipartEntity = new MultipartEntity(); multipartEntity.addPart("docLocation", fileBody); multipartEntity.addPart("mode", new StringBody("")); multipartEntity.addPart("docName", new StringBody(docName)); multipartEntity.addPart("docUrl", new StringBody(docUrl)); multipartEntity.addPart("sourceType", new StringBody(sourceType)); multipartEntity.addPart("summary", new StringBody(summary)); multipartEntity.addPart("docType", new StringBody(docType)); multipartEntity.addPart("version", new StringBody(apiVersion)); multipartEntity.addPart("apiName", new StringBody(apiName)); multipartEntity.addPart("action", new StringBody("addDocumentation")); multipartEntity.addPart("provider", new StringBody(apiProvider)); multipartEntity.addPart("mimeType", new StringBody(mimeType)); multipartEntity.addPart("optionsRadios", new StringBody(docType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); multipartEntity.addPart("optionsRadios1", new StringBody(sourceType)); httppost.setEntity(multipartEntity); //Upload created file and validate HttpResponse response = httpClient.execute(httppost); HttpEntity entity = response.getEntity(); JSONObject jsonObject1 = new JSONObject(EntityUtils.toString(entity)); assertFalse(jsonObject1.getBoolean("error"), "Error when adding files to the API "); }