已经解决,打印response的信息发现是文件为空 byteArrayInputStream.reset(); 在调用加密方法后,reset一下InputStream即可
微信支付图片上传API使用wechatpay-apache-httpclient返回400怎么办?看着git 中的 wechatpay-apache-httpclient demo,写了图片上传,但是一直返回400 bad request,求帮忙看看啥情况? 下面是代码 PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(Files.newInputStream(Paths.get(PrivateKeyPath))); //使用自动更新的签名验证器,不需要传入证书 verifier = new AutoUpdateCertificatesVerifier( new WechatPay2Credentials(tMerchantId, new PrivateKeySigner(MerchantSerialNumber, merchantPrivateKey)), ApiV3key.getBytes(StandardCharsets.UTF_8)); httpClient = WechatPayHttpClientBuilder.create() .withMerchant(MerchantId, MerchantSerialNumber, merchantPrivateKey) .withValidator(new WechatPay2Validator(verifier)) .build(); URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload"); byte[] bytes = file.getBytes(); try (InputStream byteArrayInputStream = new ByteArrayInputStream(bytes)) { String sha256 = DigestUtils.sha256Hex(byteArrayInputStream); WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri) .withImage(file.getOriginalFilename(), sha256, byteArrayInputStream) .build(); Header[] allHeaders = request.getAllHeaders(); for (Header allHeader : allHeaders) { System.out.println(allHeader); } try (CloseableHttpResponse response = httpClient.execute(request)) { if (SC_OK == response.getStatusLine().getStatusCode()){ HttpEntity entity = response.getEntity(); // do something useful with the response body // and ensure it is fully consumed String s = EntityUtils.toString(entity); System.out.println(s); return success; }else { return error; } } }
2023-02-15