mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 02:28:25 +08:00 
			
		
		
		
	🎨 优化代码,使用java8自带的Base64类
This commit is contained in:
		| @ -8,7 +8,6 @@ import com.github.binarywang.wxpay.exception.WxPayException; | |||||||
| import com.github.binarywang.wxpay.service.EntPayService; | import com.github.binarywang.wxpay.service.EntPayService; | ||||||
| import com.github.binarywang.wxpay.service.WxPayService; | import com.github.binarywang.wxpay.service.WxPayService; | ||||||
| import com.github.binarywang.wxpay.util.SignUtils; | import com.github.binarywang.wxpay.util.SignUtils; | ||||||
| import org.apache.commons.codec.binary.Base64; |  | ||||||
| import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; | import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; | ||||||
| import org.bouncycastle.jce.provider.BouncyCastleProvider; | import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||||||
| import org.bouncycastle.openssl.PEMParser; | import org.bouncycastle.openssl.PEMParser; | ||||||
| @ -22,6 +21,7 @@ import java.nio.file.Files; | |||||||
| import java.nio.file.Path; | import java.nio.file.Path; | ||||||
| import java.security.PublicKey; | import java.security.PublicKey; | ||||||
| import java.security.Security; | import java.security.Security; | ||||||
|  | import java.util.Base64; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * <pre> |  * <pre> | ||||||
| @ -168,7 +168,7 @@ public class EntPayServiceImpl implements EntPayService { | |||||||
|  |  | ||||||
|         cipher.init(Cipher.ENCRYPT_MODE, publicKey); |         cipher.init(Cipher.ENCRYPT_MODE, publicKey); | ||||||
|         byte[] encrypt = cipher.doFinal(srcString.getBytes(StandardCharsets.UTF_8)); |         byte[] encrypt = cipher.doFinal(srcString.getBytes(StandardCharsets.UTF_8)); | ||||||
|         return Base64.encodeBase64String(encrypt); |         return Base64.getEncoder().encodeToString(encrypt); | ||||||
|       } |       } | ||||||
|     } catch (Exception e) { |     } catch (Exception e) { | ||||||
|       throw new WxPayException("加密出错", e); |       throw new WxPayException("加密出错", e); | ||||||
|  | |||||||
| @ -3,7 +3,6 @@ package com.github.binarywang.wxpay.service.impl; | |||||||
| import com.github.binarywang.wxpay.bean.WxPayApiData; | import com.github.binarywang.wxpay.bean.WxPayApiData; | ||||||
| import com.github.binarywang.wxpay.exception.WxPayException; | import com.github.binarywang.wxpay.exception.WxPayException; | ||||||
| import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||||
| import jodd.util.Base64; |  | ||||||
| import me.chanjar.weixin.common.util.json.GsonParser; | import me.chanjar.weixin.common.util.json.GsonParser; | ||||||
| import org.apache.commons.lang3.StringUtils; | import org.apache.commons.lang3.StringUtils; | ||||||
| import org.apache.http.HttpEntity; | import org.apache.http.HttpEntity; | ||||||
| @ -29,6 +28,7 @@ import org.apache.http.util.EntityUtils; | |||||||
| import javax.net.ssl.SSLContext; | import javax.net.ssl.SSLContext; | ||||||
| import java.net.URI; | import java.net.URI; | ||||||
| import java.nio.charset.StandardCharsets; | import java.nio.charset.StandardCharsets; | ||||||
|  | import java.util.Base64; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * <pre> |  * <pre> | ||||||
| @ -48,7 +48,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl { | |||||||
|       try (CloseableHttpClient httpClient = httpClientBuilder.build()) { |       try (CloseableHttpClient httpClient = httpClientBuilder.build()) { | ||||||
|         try (CloseableHttpResponse response = httpClient.execute(httpPost)) { |         try (CloseableHttpResponse response = httpClient.execute(httpPost)) { | ||||||
|           final byte[] bytes = EntityUtils.toByteArray(response.getEntity()); |           final byte[] bytes = EntityUtils.toByteArray(response.getEntity()); | ||||||
|           final String responseData = Base64.encodeToString(bytes); |           final String responseData = Base64.getEncoder().encodeToString(bytes); | ||||||
|           this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseData); |           this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseData); | ||||||
|           wxApiData.set(new WxPayApiData(url, requestStr, responseData, null)); |           wxApiData.set(new WxPayApiData(url, requestStr, responseData, null)); | ||||||
|           return bytes; |           return bytes; | ||||||
|  | |||||||
| @ -1,11 +1,5 @@ | |||||||
| package com.github.binarywang.wxpay.service.impl; | package com.github.binarywang.wxpay.service.impl; | ||||||
|  |  | ||||||
| import java.net.URI; |  | ||||||
| import java.nio.charset.StandardCharsets; |  | ||||||
| import javax.net.ssl.SSLContext; |  | ||||||
|  |  | ||||||
| import org.apache.commons.lang3.StringUtils; |  | ||||||
|  |  | ||||||
| import com.github.binarywang.wxpay.bean.WxPayApiData; | import com.github.binarywang.wxpay.bean.WxPayApiData; | ||||||
| import com.github.binarywang.wxpay.exception.WxPayException; | import com.github.binarywang.wxpay.exception.WxPayException; | ||||||
| import jodd.http.HttpConnectionProvider; | import jodd.http.HttpConnectionProvider; | ||||||
| @ -15,9 +9,14 @@ import jodd.http.ProxyInfo; | |||||||
| import jodd.http.ProxyInfo.ProxyType; | import jodd.http.ProxyInfo.ProxyType; | ||||||
| import jodd.http.net.SSLSocketHttpConnectionProvider; | import jodd.http.net.SSLSocketHttpConnectionProvider; | ||||||
| import jodd.http.net.SocketHttpConnectionProvider; | import jodd.http.net.SocketHttpConnectionProvider; | ||||||
| import jodd.util.Base64; | import org.apache.commons.lang3.StringUtils; | ||||||
| import org.apache.http.client.methods.HttpPost; | import org.apache.http.client.methods.HttpPost; | ||||||
|  |  | ||||||
|  | import javax.net.ssl.SSLContext; | ||||||
|  | import java.net.URI; | ||||||
|  | import java.nio.charset.StandardCharsets; | ||||||
|  | import java.util.Base64; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 微信支付请求实现类,jodd-http实现. |  * 微信支付请求实现类,jodd-http实现. | ||||||
|  * Created by Binary Wang on 2016/7/28. |  * Created by Binary Wang on 2016/7/28. | ||||||
| @ -30,7 +29,7 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl { | |||||||
|     try { |     try { | ||||||
|       HttpRequest request = this.buildHttpRequest(url, requestStr, useKey); |       HttpRequest request = this.buildHttpRequest(url, requestStr, useKey); | ||||||
|       byte[] responseBytes = request.send().bodyBytes(); |       byte[] responseBytes = request.send().bodyBytes(); | ||||||
|       final String responseString = Base64.encodeToString(responseBytes); |       final String responseString = Base64.getEncoder().encodeToString(responseBytes); | ||||||
|       this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseString); |       this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseString); | ||||||
|       if (this.getConfig().isIfSaveApiData()) { |       if (this.getConfig().isIfSaveApiData()) { | ||||||
|         wxApiData.set(new WxPayApiData(url, requestStr, responseString, null)); |         wxApiData.set(new WxPayApiData(url, requestStr, responseString, null)); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang