List of usage examples for org.apache.http.entity.mime MultipartEntityBuilder create
public static MultipartEntityBuilder create()
From source file:org.wso2.msf4j.HttpServerTest.java
@Test public void testFormDataParamWithComplexForm() throws IOException, URISyntaxException { HttpURLConnection connection = request("/test/v1/complexForm", HttpMethod.POST); StringBody companyText = new StringBody("{\"type\": \"Open Source\"}", ContentType.APPLICATION_JSON); StringBody personList = new StringBody( "[{\"name\":\"Richard Stallman\",\"age\":63}, {\"name\":\"Linus Torvalds\",\"age\":46}]", ContentType.APPLICATION_JSON); HttpEntity reqEntity = MultipartEntityBuilder.create().addTextBody("id", "1") .addPart("company", companyText).addPart("people", personList) .addBinaryBody(//from w w w. ja v a2s. com "file", new File(Thread.currentThread().getContextClassLoader() .getResource("testTxtFile.txt").toURI()), ContentType.DEFAULT_BINARY, "testTxtFile.txt") .build(); connection.setRequestProperty("Content-Type", reqEntity.getContentType().getValue()); try (OutputStream out = connection.getOutputStream()) { reqEntity.writeTo(out); } InputStream inputStream = connection.getInputStream(); String response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); connection.disconnect(); assertEquals(response, "testTxtFile.txt:1:2:Open Source"); }
From source file:org.wso2.msf4j.HttpServerTest.java
@Test public void testFormDataParamWithMultipleFiles() throws IOException, URISyntaxException { HttpURLConnection connection = request("/test/v1/multipleFiles", HttpMethod.POST); File file1 = new File( Thread.currentThread().getContextClassLoader().getResource("testTxtFile.txt").toURI()); File file2 = new File( Thread.currentThread().getContextClassLoader().getResource("testPngFile.png").toURI()); HttpEntity reqEntity = MultipartEntityBuilder.create() .addBinaryBody("files", file1, ContentType.DEFAULT_BINARY, file1.getName()) .addBinaryBody("files", file2, ContentType.DEFAULT_BINARY, file2.getName()).build(); connection.setRequestProperty("Content-Type", reqEntity.getContentType().getValue()); try (OutputStream out = connection.getOutputStream()) { reqEntity.writeTo(out);/*from w ww. j av a 2 s . c o m*/ } InputStream inputStream = connection.getInputStream(); String response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); connection.disconnect(); assertEquals(response, "2"); }
From source file:org.wso2.msf4j.HttpServerTest.java
@Test public void testFormDataParamWithFileStream() throws IOException, URISyntaxException { HttpURLConnection connection = request("/test/v1/streamFile", HttpMethod.POST); File file = new File(Thread.currentThread().getContextClassLoader().getResource("testTxtFile.txt").toURI()); HttpEntity reqEntity = MultipartEntityBuilder.create() .addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName()).build(); connection.setRequestProperty("Content-Type", reqEntity.getContentType().getValue()); try (OutputStream out = connection.getOutputStream()) { reqEntity.writeTo(out);//from ww w.j av a 2s. c om } InputStream inputStream = connection.getInputStream(); String response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); connection.disconnect(); StringBuilder stringBuilder = new StringBuilder(); try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { while (bufferedReader.ready()) { stringBuilder.append(bufferedReader.readLine()); } } assertEquals(response, stringBuilder.toString() + "-" + file.getName()); }
From source file:org.wso2.msf4j.HttpServerTest.java
@Test public void getAllFormItemsMultipart() throws IOException, URISyntaxException { HttpURLConnection connection = request("/test/v1/getAllFormItemsMultipart", HttpMethod.POST); StringBody companyText = new StringBody("{\"type\": \"Open Source\"}", ContentType.APPLICATION_JSON); StringBody personList = new StringBody( "[{\"name\":\"Richard Stallman\",\"age\":63}, {\"name\":\"Linus Torvalds\",\"age\":46}]", ContentType.APPLICATION_JSON); HttpEntity reqEntity = MultipartEntityBuilder.create().addTextBody("id", "1") .addPart("company", companyText).addPart("people", personList) .addBinaryBody("file", new File(Thread.currentThread().getContextClassLoader().getResource("testTxtFile.txt") .toURI()),//from ww w . j a v a 2 s . c o m ContentType.DEFAULT_BINARY, "testTxtFile.txt") .addBinaryBody( "file", new File(Thread.currentThread().getContextClassLoader() .getResource("testPngFile.png").toURI()), ContentType.DEFAULT_BINARY, "testPngFile.png") .build(); connection.setRequestProperty("Content-Type", reqEntity.getContentType().getValue()); try (OutputStream out = connection.getOutputStream()) { reqEntity.writeTo(out); } InputStream inputStream = connection.getInputStream(); String response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); connection.disconnect(); assertEquals("FileCount-2 SecondFileName-testPngFile.png FirstPerson-Richard Stallman", response); connection = request("/test/v1/getAllFormItemsXFormUrlEncoded", HttpMethod.POST); String rawData = "names=WSO2&names=IBM&type=Software"; ByteBuffer encodedData = Charset.defaultCharset().encode(rawData); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", MediaType.APPLICATION_FORM_URLENCODED); connection.setRequestProperty("Content-Length", String.valueOf(encodedData.array().length)); try (OutputStream os = connection.getOutputStream()) { os.write(Arrays.copyOf(encodedData.array(), encodedData.limit())); } inputStream = connection.getInputStream(); response = StreamUtil.asString(inputStream); IOUtils.closeQuietly(inputStream); connection.disconnect(); assertEquals("Type = Software No of names = 2 First name = IBM", response); }
From source file:xin.nic.sdk.registrar.util.HttpUtil.java
public static String doPost(String requestUrl, Map<String, String> paramsMap, Map<String, InputStream> files) throws Exception { RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(500) .setSocketTimeout(20000).setConnectTimeout(20000).build(); // ?/*from w w w.ja v a 2s.co m*/ MultipartEntityBuilder mbuilder = MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(Charset.forName(charset)); if (paramsMap != null) { Set<Entry<String, String>> paramsSet = paramsMap.entrySet(); for (Entry<String, String> entry : paramsSet) { mbuilder.addTextBody(entry.getKey(), entry.getValue(), ContentType.create("text/plain", charset)); } } if (files != null) { Set<Entry<String, InputStream>> filesSet = files.entrySet(); for (Entry<String, InputStream> entry : filesSet) { ByteArrayOutputStream os = new ByteArrayOutputStream(); InputStream is = entry.getValue(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) > 0) { os.write(buffer, 0, len); } os.close(); is.close(); mbuilder.addBinaryBody("attachment", os.toByteArray(), ContentType.APPLICATION_OCTET_STREAM, entry.getKey()); } } HttpPost httpPost = new HttpPost(requestUrl); httpPost.setConfig(requestConfig); HttpEntity httpEntity = mbuilder.build(); httpPost.setEntity(httpEntity); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; return httpClient.execute(httpPost, responseHandler); }