springboot jar 部署到linux之后 获取类资源文件问题-- 仅限linux 下 情况比较特殊 需要获取打到jar内的 讲台资源 只能通过流获取,根据路径获取不到指定文件 nullpointExption

https://blog.csdn.net/qq_27000425/article/details/72897282

 

ClassPathResource类,如果没有指定相对的类名,该类将从类的根路径开始寻找某个resource,如果指定了相对的类名,则根据指定类的相对路径来查找某个resource。

Resource rs = new ClassPathResource("onlyfun/caterpillar/beans-config.xml");
或者
Resource rs = new ClassPathResource("beans-config.xml",SpringDemo.class);

--------------------- 本文来自 qq_27000425 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_27000425/article/details/72897282?utm_source=copy 

 

 

 

/**
*任意文件下载 只能通过
* @author zhangyh
* @date 2018/9/21 17:56
* @param [request, response, url]
* @return void
*
*/
public void downloadStream(HttpServletRequest request,HttpServletResponse response, InputStream inputStream,String fileName) {
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(inputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()));
//response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}

}
posted @ 2018-09-25 18:52  代码让自己变强  阅读(1029)  评论(0编辑  收藏  举报