接收到图片以二进制参数形式作为body参数发送post请求
@RequestMapping("/postUrl")
public String postUrl(@RequestBody MultipartFile file) throws IOException {
byte[] bytes = file.getBytes();
String postMothed = postMothed(url, bytes, header);
return postMothed;
}
/**
*
* @param url 路径地址
* @param param 二进制图片参数
* @param header 请求头
* @return
*/
public String postMothed(String url, byte[] param, String header) {
// 获得Http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建post请求
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", header);
// 转换二进制字节参数
httpPost.setEntity(new ByteArrayEntity(param));
String body = "";
// 由客户端执行(发送)Post请求
try {
CloseableHttpResponse execute = httpClient.execute(httpPost);
HttpEntity entity = execute.getEntity();
if (entity!=null){
body = EntityUtils.toString(entity, "utf-8");
}
} catch (IOException e) {
e.printStackTrace();
}
return body;
}