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.brightcove.com.uploader.helper.MediaManagerHelper.java
public Long uploadFile(URI uri, File f, DefaultHttpClient client) throws HttpException, IOException, URISyntaxException, ParserConfigurationException, SAXException { mLog.info("using " + uri.getHost() + " on port " + uri.getPort() + " for MM upload"); HttpPost method = new HttpPost(uri); MultipartEntity entityIn = new MultipartEntity(); entityIn.addPart("FileName", new StringBody(f.getName(), Charset.forName("UTF-8"))); FileBody fileBody = null;/*w w w .j a va2 s .c o m*/ if (f != null) { fileBody = new FileBody(f); } if (f != null) { entityIn.addPart(f.getName(), fileBody); } entityIn.addPart("Upload", new StringBody("Submit Query", Charset.forName("UTF-8"))); method.setEntity(entityIn); if (client != null) { return executeUpload(method, client); } return null; }
From source file:io.undertow.servlet.test.multipart.MultiPartTestCase.java
@Test public void testMultiPartRequestWithNoMultipartConfig() throws IOException { TestHttpClient client = new TestHttpClient(); try {// w w w.j a v a 2 s . c o m String uri = DefaultServer.getDefaultServerURL() + "/servletContext/0"; HttpPost post = new HttpPost(uri); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8)); entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile()))); post.setEntity(entity); HttpResponse result = client.execute(post); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); final String response = HttpClientUtils.readResponse(result); Assert.assertEquals("PARAMS:\n", response); } finally { client.getConnectionManager().shutdown(); } }
From source file:io.undertow.servlet.test.multipart.MultiPartTestCase.java
@Test public void testMultiPartIndividualFileToLarge() throws IOException { TestHttpClient client = new TestHttpClient(); try {/* w w w .j a v a 2 s .c o m*/ String uri = DefaultServer.getDefaultServerURL() + "/servletContext/3"; HttpPost post = new HttpPost(uri); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8)); entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile()))); post.setEntity(entity); HttpResponse result = client.execute(post); String response = HttpClientUtils.readResponse(result); Assert.assertEquals("TEST FAILED: wrong response code\n" + response, StatusCodes.INTERNAL_SERVER_ERROR, result.getStatusLine().getStatusCode()); } finally { client.getConnectionManager().shutdown(); } }
From source file:org.jacocoveralls.jacocoveralls.JacocoverallsMojo.java
public void sendToCoveralls() throws MojoExecutionException { try {/*from w ww . jav a 2 s.c o m*/ FileBody data = new FileBody(targetFile, "application/octet-stream", sourceEncoding); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(COVERALLS_API); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("json_file", data); httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost); if ("HTTP/1.1 200 OK".equals(response.getStatusLine())) { getLog().info("Data has been sent to coveralls."); } else { getLog().error("Error sending data to coveralls."); } } catch (UnsupportedEncodingException e) { throw new MojoExecutionException("Error sending data do Coveralls.", e); } catch (ClientProtocolException e) { throw new MojoExecutionException("Error sending data do Coveralls.", e); } catch (IOException e) { throw new MojoExecutionException("Error sending data do Coveralls.", e); } }
From source file:br.itecbrazil.serviceftpcliente.model.ThreadEnvio.java
private void enviarArquivo(File arquivo, Config config) { if (gravarBackup(arquivo)) { logger.info("Enviando arquivo " + arquivo.getName() + ". Thread: " + Thread.currentThread().getName()); try {/*from w ww . jav a2s. c o m*/ HttpClient httpclient = new org.apache.http.impl.client.DefaultHttpClient(); HttpPost httppost = new HttpPost("http://" + config.getHost() + "/upload"); httppost.setHeader("X-Requested-With", "XMLHttpRequest"); MultipartEntity mpEntity = new MultipartEntity(); mpEntity.addPart("id", new StringBody(config.getUsuario())); mpEntity.addPart("arquivo", new FileBody(arquivo)); httppost.setEntity(mpEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); if (response.getStatusLine().getStatusCode() == 200) { logger.info("Arquivo arquivo " + arquivo.getName() + " enviado com sucesso. Thread: " + Thread.currentThread().getName()); atualizarDadoDeEnvio(arquivo); arquivo.delete(); logger.info("Arquivo deletado " + arquivo.getName() + " do diretorio de envio. Thread: " + Thread.currentThread().getName()); } } catch (MalformedURLException ex) { loggerExceptionEnvio.info(ex); } catch (IOException ex) { loggerExceptionEnvio.info(ex); } } }
From source file:io.undertow.servlet.test.multipart.MultiPartTestCase.java
@Test public void testMultiPartRequestToLarge() throws IOException { TestHttpClient client = new TestHttpClient(); try {/*from w w w . j ava 2 s. c o m*/ String uri = DefaultServer.getDefaultServerURL() + "/servletContext/2"; HttpPost post = new HttpPost(uri); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8)); entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile()))); post.setEntity(entity); HttpResponse result = client.execute(post); Assert.assertEquals(DefaultServer.isH2() || DefaultServer.isAjp() ? StatusCodes.SERVICE_UNAVAILABLE : StatusCodes.INTERNAL_SERVER_ERROR, result.getStatusLine().getStatusCode()); HttpClientUtils.readResponse(result); } catch (IOException expected) { //in some environments the forced close of the read side will cause a connection reset } finally { client.getConnectionManager().shutdown(); } }
From source file:org.dataconservancy.ui.it.support.DepositRequest.java
public HttpPost asHttpPost() { if (!dataSetSet) { throw new IllegalStateException("DataItem not set: call setDataSet(DataItem) first."); }/*from w w w . ja v a 2 s. co m*/ if (fileToDeposit == null) { throw new IllegalStateException("File not set: call setFileToDeposit(File) first"); } if (collectionId == null || collectionId.isEmpty()) { throw new IllegalStateException("Collection id not set: call setCollectionId(String) first."); } if (isUpdate && (dataItemIdentifier == null || dataItemIdentifier.isEmpty())) { throw new IllegalStateException( "Identifer is not set Identifier must be set: callSetDataSetIdentifier or pass in an ID in the constructor"); } if (null == packageId) packageId = ""; String depositUrl = urlConfig.getDepositUrl().toString() + "?redirectUrl=/pages/usercollections.jsp?currentCollectionId=" + collectionId; HttpPost post = new HttpPost(depositUrl); MultipartEntity entity = new MultipartEntity(); try { entity.addPart("currentCollectionId", new StringBody(collectionId, Charset.forName("UTF-8"))); entity.addPart("dataSet.name", new StringBody(name, Charset.forName("UTF-8"))); entity.addPart("dataSet.description", new StringBody(description, Charset.forName("UTF-8"))); entity.addPart("dataSet.id", new StringBody(dataItemIdentifier, Charset.forName("UTF-8"))); entity.addPart("depositPackage.id", new StringBody(packageId, Charset.forName("UTF-8"))); entity.addPart("isContainer", new StringBody(Boolean.valueOf(isContainer()).toString(), Charset.forName("UTF-8"))); if (isUpdate) { entity.addPart("datasetToUpdateId", new StringBody(dataItemIdentifier, Charset.forName("UTF-8"))); entity.addPart(STRIPES_UPDATE_EVENT, new StringBody("Update", Charset.forName("UTF-8"))); } else { entity.addPart(STRIPES_DEPOSIT_EVENT, new StringBody("Deposit", Charset.forName("UTF-8"))); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } FileBody fileBody = new FileBody(fileToDeposit); entity.addPart("uploadedFile", fileBody); post.setEntity(entity); return post; }
From source file:io.undertow.servlet.test.multipart.MultiPartTestCase.java
@Test public void testMultiPartRequestWithAddedServlet() throws IOException { TestHttpClient client = new TestHttpClient(); try {//from w w w . j a va 2 s . c o m String uri = DefaultServer.getDefaultServerURL() + "/servletContext/added"; HttpPost post = new HttpPost(uri); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("formValue", new StringBody("myValue", "text/plain", StandardCharsets.UTF_8)); entity.addPart("file", new FileBody(new File(MultiPartTestCase.class.getResource("uploadfile.txt").getFile()))); post.setEntity(entity); HttpResponse result = client.execute(post); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); final String response = HttpClientUtils.readResponse(result); Assert.assertEquals("PARAMS:\n" + "name: formValue\n" + "filename: null\n" + "content-type: null\n" + "Content-Disposition: form-data; name=\"formValue\"\n" + "size: 7\n" + "content: myValue\n" + "name: file\n" + "filename: uploadfile.txt\n" + "content-type: application/octet-stream\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"uploadfile.txt\"\n" + "Content-Type: application/octet-stream\n" + "size: 13\n" + "content: file contents\n", response); } finally { client.getConnectionManager().shutdown(); } }
From source file:hu.sztaki.lpds.portal.util.stream.HttpClient.java
/** * File upload/*from w ww . j ava 2s . c o m*/ * @param pFile file to be uploaded * @param uploadName upload naem * @param pValue parameters used during the connection * @return http answer code * @throws java.lang.Exception communication error */ public int fileUpload(File pFile, String uploadName, Hashtable pValue) throws Exception { int res = 0; MultipartEntity reqEntity = new MultipartEntity(); // file parameter if (uploadName != null) { FileBody bin = new FileBody(pFile); reqEntity.addPart(uploadName, bin); } //text parameters Enumeration<String> enm = pValue.keys(); String key; while (enm.hasMoreElements()) { key = enm.nextElement(); reqEntity.addPart(key, new StringBody("" + pValue.get(key))); } httpPost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httpPost); HttpEntity resEntity = response.getEntity(); res = response.getStatusLine().getStatusCode(); close(); return res; }