/**
* @param file 本地文件对象
* @param url 接口路径
*/
public static void UploadFileByHttpClient(File file, String url) {
String result="";
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 把文件加到HTTP的post请求中
builder.addBinaryBody("files",new FileInputStream(file),ContentType.APPLICATION_OCTET_STREAM,file.getName());
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
HttpEntity responseEntity = response.getEntity();
result = EntityUtils.toString(responseEntity, "UTF-8");
}catch (Exception e){
e.printStackTrace();
}
System.out.println(result);
}
09-18
1006
08-26
484
05-24
1134
07-21
2949