我們在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();
}
完美運行並訪問