特约商户进件需要使用上传图片API,我用上传图片的API上传返回400,麻烦帮忙看一下
这是写的方法:
URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload");
try (FileInputStream ins1 = (FileInputStream) file.getInputStream()) {
String sha256 = DigestUtils.sha256Hex(ins1);
try (InputStream ins2 = file.getInputStream()) {
HttpPost request = new WechatPayUploadHttpPost.Builder(uri)
.withImage(file.getOriginalFilename(), sha256, ins2)
.build();
// 加载商户私钥(privateKey:私钥字符串)
PrivateKey merchantPrivateKey = PemUtil
.loadPrivateKey(new ByteArrayInputStream(getPrivateKey().getBytes("utf-8")));
// 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),apiV3Key.getBytes("utf-8"));
// 初始化httpClient
CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withValidator(new WechatPay2Validator(verifier)).build();
String wechatpaySerial = verifier.getValidCertificate().getSerialNumber().toString(16);
JSONObject requestData = new JSONObject();
requestData.put("filename",file.getOriginalFilename());
requestData.put("sha256",sha256);
StringEntity entity = new StringEntity(JSONObject.toJSONString(requestData),"utf-8");
String token = getToken("POST","/v3/merchant/media/upload",JSONObject.toJSONString(requestData));
entity.setContentType("application/json");
request.setEntity(entity);
request.setHeader("Authorization","WECHATPAY2-SHA256-RSA2048\n"+token);
// request.setHeader("Accept","application/json");
request.setHeader("Content-Type","multipart/form-data");
request.setHeader("Wechatpay-Serial", wechatpaySerial);
CloseableHttpResponse response = httpClient.execute(request);
try {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) { //处理成功
System.out.println("success,return body = " + EntityUtils.toString(response.getEntity()));
return AjaxResult.success("上传成功",EntityUtils.toString(response.getEntity()));
} else if (statusCode == 204) { //处理成功,无返回Body
System.out.println("success");
return AjaxResult.success("上传成功");
} else {
String returnBody = EntityUtils.toString(response.getEntity());
System.out.println("failed,resp code = " + statusCode+ ",return body = " + returnBody);
return AjaxResult.error("上传失败",JSONObject.parseObject(returnBody).getString("message"));
// throw new IOException("request failed");
}
}catch (Exception e){
e.printStackTrace();
throw new IOException("request failed");
}
finally {
response.close();
}
}
}
这是返回的报错信息:
status code[400] request headers[[Accept: application/json; charset=UTF-8, Authorization: WECHATPAY2-SHA256-RSA2048 mchid="XXXXXXXX",nonce_str="gsdfgsfwretwer",timestamp="1710405669",serial_no="XXXXXXXXXXXXXXXXXXXXXXX",signature="Uj33Uwduo/7bbG1vgmcvtr/429eBX8gmpEjGkYyN1ax6C46XUaBvaXhXXXXXXXXXXXXXXXXXXXXXXX4CW9KVz82P0gaAxbaePVhc/AGylgrqiMOqcUPWJ6Sos+bvAUA1sbwzWseSmPGwJ3vBaUbL7bjnyRYVAGwT2iTbdL712lmKD7zK5D0SN/qQGKo3m4BVpd8ArnP3z/fbGO8FjtrY+l1povSpVlXdNQK2L0Pv7bNFWoXXbZojTTCRvVhIL7fisHnLCws44M0pl3LYqhi5sQej36f1LFyxrHRCihX/xnHzKDplEXAV5+/n7NJZMnfAtqZZRKrPEg==", Content-Type: multipart/form-data, Wechatpay-Serial: XXXXXXXXXXXXXXXXXXXXXXXXXXXX, Authorization: WECHATPAY2-SHA256-RSA2048 mchid="111111111",nonce_str="QaHV2w2cqAGAchJNOEQlu8CLwh8JfD1k",timestamp="1710405669",serial_no="XXXXXXXXXXXXXXXXXXXXXXXXXXXXX",signature="taDsR+TQEnv40SnGGmIw6Y6Uc3oNdOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXhUShPyFgUJ9SCBvCa3N0BY5fGVt9H1ctPRNNDKzoovpyAfiTjrr0FSyEr9wpGroV7iKJ09mwMgblgBvdxqLXV4qsUS7ME0QaKplT8HtpEMRwLNKJ8S2DCtwkUEE03tjKHJMae7+ZgaIMYsrq20EErJi/mHPCJNczfjLJIrjtdYVUw6gUcmwA9VsjLrYD5wpqIv4/QD0GnjiltO/qYBkhrMjf8JL5LE25LRR0JmQfMTT6Q==", Content-Length: 99, Host: api.mch.weixin.qq.com, Connection: Keep-Alive, User-Agent: WechatPay-Apache-HttpClient/0.4.9 (Windows 10/10.0) Java/1.8.0_371, Accept-Encoding: gzip,deflate]]
failed,resp code = 400,return body = <html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
建议使用sdk吧