微信证书存储在数据库中
File file = new File("./apiclient_cert.p12");
InputStream certStream = new FileInputStream(file);
byte[] certData = new byte[(int) file.length()];
certStream.read(certData);
certStream.close();
// 把字节数组保存成String 存储在数据库
String encoded = Base64.getEncoder().encodeToString(certData);
// 再次使用时反编回来
byte[] decoded = Base64.getDecoder().decode(encoded);
重写WXPayConfig类
public class WXPayConfigImpl implements WXPayConfig {
private String appId;
private String mchId;
private String key;
private byte[] certData;
public WXPayConfigImpl(String appId, String mchId, String key, String certString) {
this.appId = appId;
this.mchId = mchId;
this.key = key;
this.certData = Base64.getDecoder().decode(certString);
}
@Override
public String getAppID() {
return this.appId;
}
@Override
public String getMchID() {
return this.mchId;
}
@Override
public String getKey() {
return this.key;
}
@Override
public InputStream getCertStream() {
ByteArrayInputStream certBis;
certBis = new ByteArrayInputStream(this.certData);
return certBis;
}
@Override
public int getHttpConnectTimeoutMs() {
return 2000;
}
@Override
public int getHttpReadTimeoutMs() {
return 10000;
}
}