package com.dsh.guns.modular.system.util; import com.github.binarywang.wxpay.v3.WechatPayUploadHttpPost; import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder; import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier; import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner; import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials; import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator; import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.time.DateUtils; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.security.PrivateKey; import java.text.ParseException; import java.util.Date; import static org.apache.http.HttpStatus.SC_OK; import static org.junit.Assert.assertEquals; /** * 定时任务工具类 */ @Component public class WxUploadImgUtil { // 你的商户私钥 private static final String privateKey = "-----BEGIN PRIVATE KEY-----\n" + "-----END PRIVATE KEY-----\n"; //测试AutoUpdateCertificatesVerifier的verify方法参数 private static final String serialNumber = ""; private static final String message = ""; private static final String signature = ""; private static final String merchantId = ""; // 商户号 private static final String merchantSerialNumber = ""; // 商户证书序列号 private static final String apiV3Key = ""; // API V3密钥 private CloseableHttpClient httpClient; private AutoUpdateCertificatesVerifier verifier; public String uploadImg(String date, String pattern) throws IOException, URISyntaxException { PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey); //使用自动更新的签名验证器,不需要传入证书 verifier = new AutoUpdateCertificatesVerifier( new WechatPay2Credentials(merchantId, new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)), apiV3Key.getBytes(StandardCharsets.UTF_8)); httpClient = WechatPayHttpClientBuilder.create() .withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey) .withValidator(new WechatPay2Validator(verifier)) .build(); String filePath = "/your/home/hellokitty.png"; URI uri = new URI("https://api.mch.weixin.qq.com/v3/merchant/media/upload"); File file = new File(filePath); try (FileInputStream fileIs = new FileInputStream(file)) { String sha256 = DigestUtils.sha256Hex(fileIs); try (InputStream is = new FileInputStream(file)) { WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri) .withImage(file.getName(), sha256, is) .build(); try (CloseableHttpResponse response = httpClient.execute(request)) { assertEquals(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); } } } httpClient.close(); return null; } }