【梦辛工作室】微信支付V3之下载图片

大家好哇,我是梦辛工作室的灵,最近有小伙伴是对接微信支付v3投诉下载的时候老遇到各种奇葩问题,我在这里给大家讲解下,这个接口是不需要验证签名的,但微信开发文档又不仔细说明怎么不验证签名,就把大家伙搞的脑袋大,我这边找到了方法,目前分享给大家,以免再次到处找不到资料,具体代码如下:

	WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
				.withMerchant(mchId, mchSerialNo, merchantPrivateKey)
				//设置响应对象无需签名
				.withValidator((response) -> true);

		httpNoSignPayClient = builder.build();

这样就获取了一个不会有验证签名的Client,调用就可以了

/**
	 * 下载图片
	 *
	 * @param media_url
	 *            图片路径
	 * @return
	 * @throws Exception
	 */
	public BufferedImage downLoadImg(String media_url, String filePath) throws Exception {

		if (!isStep) {
			errorHint = "未成功启用Step";
			return null;
		}

		HttpGet httpGet = new HttpGet(media_url);
		httpGet.setHeader("Accept", "application/json");

		CloseableHttpResponse response = httpNoSignPayClient.execute(httpGet);

		BufferedImage result = null;

		try {
			HttpEntity responseEntity = response.getEntity();
			InputStream content = responseEntity.getContent();
			result = ImageIO.read(content);
			if(filePath != null && filePath.length() >= 0){
				File file = new File(filePath);
				if(!file.exists()){
					file.createNewFile();
				}
				FileOutputStream fileOutputStream = new FileOutputStream(file);
				int bytesWritten = 0;
				int byteCount = 0;
				byte[] bytes = new byte[1024];
				while ((byteCount = content.read(bytes)) != -1)
				{
					fileOutputStream.write(bytes, bytesWritten, byteCount);
					bytesWritten += byteCount;
				}
				content.close();
				fileOutputStream.close();
			}
		} finally {
			response.close();
		}

		return result;
	}

github传送门:https://github.com/wintton/MxWxPayUtil.git

你可能感兴趣的:(日常问题记录,java,微信)