<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
public void upload(String localFile){
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String url = "http://...";
try {
httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url );
httpPost.setConfig(RequestConfig.custom().setConnectTimeout(1000).build());
FileBody bin = new FileBody(new File(localFile));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", bin)
.build();
httpPost.setEntity(reqEntity);
response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
}
EntityUtils.consume(resEntity);
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(response != null){
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(httpClient != null){
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}