//传文件到远程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();
}
Java 传文件到后端远程post接口
最新推荐文章于 2023-09-04 10:58:21 发布