public void post() throws ClientProtocolException, IOException{
//创建client
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//创建post请求
HttpPost httppost = new HttpPost(url);
//文件
FileBody file1 = new FileBody(new File("F:/download/bc09df07e24c253cc6de4605a9eea07b.jpg"));
FileBody file2 = new FileBody(new File("F:/download/3feededd6b766d14267eef4c429943c1.jpg"));
//参数
//
StringBody lng = new StringBody("33.33", ContentType.TEXT_PLAIN);
StringBody lat = new StringBody("44.33", ContentType.TEXT_PLAIN);
//组建访问实体
//如 spring-mvc @RequestParam("lng")Double lng
// @RequestParam("lat")Double lat
// @RequestParam("file")MultipartFile[] files
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", file1)
.addPart("file", file2)
.addPart("lng", lng)
.addPart("lat", lat)
.build();
//设置实体
httppost.setEntity(reqEntity);
//设置头信息
httppost.setHeader("module", "love");
httppost.setHeader("code", "111111");
System.out.println("executing request " + httppost.getRequestLine());
//执行post请求
CloseableHttpResponse response = httpclient.execute(httppost);
try {
System.out.println("----------------------------------------");
//状态
System.out.println(response.getStatusLine());
//响应实体
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
//关闭HttpEntity流
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
转载于:https://my.oschina.net/u/2385255/blog/552180