@ApiOperation("上传电子发票文件")
@PostMapping("uploadFapiaoFile")
public Result uploadFapiaoFile() throws IOException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, InvalidKeyException {
String url = wxPay.getDomain() + "/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file";
HttpUrl parse = HttpUrl.parse(url);
FileInputStream fis = new FileInputStream("E:\\online-consultation\\处方.pdf");
StringBuilder sb = new StringBuilder();
int data;
while ((data = fis.read()) != -1) {
sb.append((char) fis.read());
}
byte[] bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
String sm3LowerCase = SM3.digest(sb.toString()).toLowerCase();
log.info("SM3[{}]", sm3LowerCase);
JSONObject meta = new JSONObject();
meta.put("digest", sm3LowerCase);
meta.put("digest_algorithm", "SM3");
meta.put("file_type", "PDF");
String signatureString = WxSignatureUtil.getSignatureString("POST", parse, meta.toString());
log.info("signature[{}]", signatureString);
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "file", RequestBody.create(MediaType.parse("pdf/plain"), sb.toString().getBytes(StandardCharsets.UTF_8)))
.addFormDataPart("meta", "meta", RequestBody.create(MediaType.parse("application/json"), meta.toString()))
.build();
OkHttpClient client = new OkHttpClient();
Request build = new Request.Builder()
.addHeader("Content-Type", "multipart/form-data")
.addHeader("Authorization", signatureString)
.addHeader("Accept","application/json")
.url(url)
.post(multipartBody)
.build();
Response response = client.newCall(build).execute();
ResponseBody body = response.body();
String string = body.string();
return Result.ok(string);
}
{
“代码”: “SIGN_ERROR”,
“详细信息”: {
“详细信息”: {
“issue”: “sign not match”
},
“field”: “签名”,
“location”: “授权”,
“sign_information”:{
“method”: “发布”,
“sign_message_length”:116,
“truncated_sign_message”: “POST\n/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file\n1722237276\nKhFTsY2fM2rsnhSu3Dm28CdpSwBWYf1B\n\n”,
“url”: “/v3/new-tax-control-fapiao/fapiao-applications/upload-fapiao-file”
}
},
“message”: “错误的签名,验签失败”
}
https://developers.weixin.qq.com/community/pay/article/doc/000ae03c54cf68daa7802f34d61813
https://developers.weixin.qq.com/community/develop/article/doc/00082aa48cc8d04629f9ee1ee51813
可以参考类似的上传图片文章指引进行调整