我们在jar部署的时候无法读取外部的文件,或者说很难读取到其他文件。还有就是根目录下无法读取并返回流文件。下面分别以微信支付证书文件和读取并返回微信业务域名、JS接口安全域名、网页授权域名文件的验证为例进行讲解。
1、微信支付证书文件apiclient_cert.p12
比如我们的apiclient_cert.p12在如下目录
linux下:
windows下:
下面直接看程序
public WeConfig() throws Exception {
//String path = "C:";//windows下写法
String path = "/datas/doc***e/mem**duce";//此处为自己实际路径
String certPath = path+"/apiclient_cert.p12";
File file = new File(certPath);
InputStream certStream = new FileInputStream(file);
this.certData = new byte[(int) file.length()];
certStream.read(this.certData);
certStream.close();
}
这样就可以读取了
2、微信业务域名、JS接口安全域名、网页授权域名文件的验证
@RequestMapping("/{filename}")
public void zulltest(HttpServletRequest request, HttpServletResponse response, @PathVariable("filename") String fileName) throws Exception{
String path = "/datas/zb";
//String path = "C:\\Users\\Administrator\\Desktop";
String certPath = path+"/"+fileName+".txt";
File file = new File(certPath);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
byte [] b = new byte[10];
int i = 0 ;
while(( i = fileInputStream.read(b)) != -1){
bufferedOutputStream.write(b,0,i);
}
fileInputStream.close();
bufferedOutputStream.close();
}
完美运行并访问