近期做了一个微信支付退款接口,需要证书双向验证,测试的时候放在resource下
起初MyConfig中我是这样,在本机中运行没有问题
public MyConfig() throws Exception {
File file = (ResourceUtils.getFile("apiclient_cert.p12"));
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
但到Linux服务器中运行就IO异常了,查阅资料可能原因是内嵌web容器访问的是jar包
解决方法1:
//创建
private byte[] certData;
public InputStream getCertStream() {
try {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("apiclient_cert.p12");
this.certData = IOUtils.toByteArray(inputStream);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return new ByteArrayInputStream(this.certData);
}
解决方法2:
public InputStream getCertStream() {
try {
ClassPathResource classPathResource = new ClassPathResource("apiclient_cert.p12");
//获取文件流
InputStream inputStream = classPathResource.getInputStream();
this.certData = IOUtils.toByteArray(inputStream);
inputStream.read(this.certData);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}