//传文件到远程post接口
public static String sendPostWithFile(File file, String urlStr) throws Exception{
MultipartEntityBuilder builder = MultipartEntityBuilder.create().addPart("file", new FileBody(file));
HttpEntity multipartEntity = builder.build();
HttpPost request = new HttpPost(urlStr); //远程接口
request.setEntity(multipartEntity);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(request);
//获取接口返回值
InputStream is = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer bf = new StringBuffer();
String line= "";
while ((line = in.readLine()) != null) {
bf.append(line);
}
System.out.println("发送消息收到的返回:"+bf.toString());
return bf.toString();
}
12-24
317
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-30
9712
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)