public void updateImage(File file){
try {
// 换行符
String LINE_END ="\r\n";
String PREFIX ="--";
// 定义数据分隔线
String BOUNDARY ="boundary";
String filename = file.getName();//文件名
FileInputStream fileInputStream = new FileInputStream(file);
String fileSha256 = DigestUtils.sha256Hex(fileInputStream);//文件sha256值
JSONObject jsonObject = new JSONObject();
jsonObject.put("filename",filename);
jsonObject.put("sha256",fileSha256);
String url = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";
log.debug("request url:{}", url);
HttpPost httpPost = new HttpPost(url);
StringEntity json = new StringEntity(jsonObject.toString(), Charset.forName("UTF-8"));
httpPost.setEntity(json);
String authorization = signatureUtil.getAuthorization(httpPost, privateKey, merchantId, certificateSerialNumber);
URL url1 =new URL("https://api.mch.weixin.qq.com/v3/merchant/media/upload");
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
// 设置为POST
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求头参数
conn.setRequestProperty("Charsert","UTF-8");
conn.setRequestProperty("Accept","application/json");
conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
conn.setRequestProperty("Authorization", authorization);
conn.setRequestProperty("user-agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36");
DataOutputStream dos =new DataOutputStream(conn.getOutputStream());
//拼装请求内容第一部分
StringBuilder strSb1 =new StringBuilder();
strSb1.append(PREFIX).append(BOUNDARY).append(LINE_END)
.append("Content-Disposition: form-data; name=\"meta\";" + LINE_END)
.append("Content-Type: application/json; " + LINE_END)
.append(LINE_END)// 空行
.append("{\"filename\":\""+filename+"\",\"sha256\":\""+fileSha256+"\"}")
.append(LINE_END);
dos.write(strSb1.toString().getBytes());
dos.flush();
//拼装请求内容第二部分
StringBuilder fileSbStart =new StringBuilder();
fileSbStart.append(PREFIX).append(BOUNDARY).append(LINE_END)
.append("Content-Disposition: form-data; name=\"file\"; filename=\""+ filename+"\";" + LINE_END)
.append("Content-Type: image/jpg" + LINE_END)
.append(LINE_END);// 空行
dos.write(fileSbStart.toString().getBytes());
dos.flush();
//文件二进制内容
InputStream is =new FileInputStream(file);
byte[] buffer =new byte[1024];
int len =0;
while ((len = is.read(buffer)) != -1){
dos.write(buffer,0,len);
}
is.close();
//拼装请求内容结尾
StringBuilder fileSbEnd1 =new StringBuilder();
fileSbEnd1.append(LINE_END)
.append(PREFIX).append(BOUNDARY).append(PREFIX)
.append(LINE_END);
dos.write(fileSbEnd1.toString().getBytes());
dos.flush();
dos.close();
//接收返回
//打印返回头信息
System.out.println("接口返回头信息:");
Map<String, List<String>> responseHeader = conn.getHeaderFields();
for (String s : responseHeader.keySet()) {
System.out.println(s);
}
//打印返回内容
int responseCode = conn.getResponseCode();
String result = new BufferedReader(new InputStreamReader(conn.getInputStream()))
.lines().collect(Collectors.joining(System.lineSeparator()));
System.out.println(result);
System.out.println(responseCode);
// log.info("updateImage result ->{}", JSONObject.toJSONString(result));
} catch (IOException e) {
e.printStackTrace();
}
}