Skip to content

根据优化建议修改 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 19, 2022
6 changes: 5 additions & 1 deletion core/src/main/java/com/wechat/pay/java/core/RSAConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ public RSAConfig build() {
requireNonNull(privateKey);
requireNonNull(merchantSerialNumber);
requireNonNull(merchantId);
requireNonNull(wechatPayCertificates);
if (wechatPayCertificates == null || wechatPayCertificates.size() == 0) {
throw new IllegalArgumentException(
"Build RSAConfig, wechatPayCertificates is empty.Please "
+ "call wechatPayCertificates() or wechatPayCertificatesFromPath() method.");
}
X509Certificate latestCertificate = null;
// 获取最近可用的微信支付平台证书
for (X509Certificate x509Certificate : wechatPayCertificates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ServiceException extends WechatPayException {
public ServiceException(HttpRequest httpRequest, int httpStatusCode, String responseBody) {
super(
String.format(
"Wrong HttpStatusCode[%d]\nhttResponseBody[%.1024s]\tHttpRequest[%s]",
"Wrong HttpStatusCode[%d]%nhttResponseBody[%.1024s]\tHttpRequest[%s]",
httpStatusCode, responseBody, httpRequest));
this.httpRequest = httpRequest;
this.httpStatusCode = httpStatusCode;
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/com/wechat/pay/java/core/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public static String toString(InputStream inputStream) throws IOException {
* @throws IOException 读取字节失败、关闭流失败等
*/
public static String loadStringFromPath(String path) throws IOException {
return toString(Files.newInputStream(Paths.get(path)));
try (InputStream inputStream = Files.newInputStream(Paths.get(path))) {
return toString(inputStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
Expand Down Expand Up @@ -112,7 +113,7 @@ public static X509Certificate loadX509FromPath(String certificatePath) {
public static X509Certificate loadX509FromString(String certificateString) {
X509Certificate certificate = null;
try (ByteArrayInputStream inputStream =
new ByteArrayInputStream(certificateString.getBytes())) {
new ByteArrayInputStream(certificateString.getBytes(StandardCharsets.UTF_8))) {
certificate = loadX509FromStream(inputStream);
} catch (IOException e) {
logger.warn("Get certificate from certificate string,inputStream close failed.", e);
Expand Down