SpringBoot 工程打成jar包后读取resources下的静态文件。
比如:在resources下有个template文件夹,里边有个test.doc文档。
public static InputStream getTemplateStream(String url) throws IOException{
//返回读取指定资源的输入流
ClassPathResource cpr = new ClassPathResource(url);
return cpr.getInputStream();
}
public static void main(String[] args) {
// 采用相对路径。如果写成“/template/test.doc” 是获取不到的。
String url = "template/test.doc";
InputStream in = null;
try {
// 获取文件流
in = getTemplateStream(url);
// TODO 下边就是你自己的业务操作了
} catch (IOException e) {
e.printStackTrace();
}finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}