1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/upload");
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, filename); builder.addTextBody("name", name);
HttpEntity entity = builder.build(); httpPost.setEntity(entity); CloseableHttpResponse response = httpClient.execute(httpPost); Integer httpCode = response.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK != httpCode) { throw new RuntimeException("failed..."); }
String respEntity = EntityUtils.toString(response.getEntity());
|