SpringBoot读取Resource下文件的几种方式(转)
转自:https://www.jianshu.com/p/7d7e5e4e8ae3
最近在项目中涉及到Excle的导入功能,通常是我们定义完模板供用户下载,用户按照模板填写完后上传;这里待下载模板位置为resource/excelTemplate/test.xlsx,尝试了四种读取方式,并且测试了四种读取方式分别的windows开发环境下(IDE中)读取和生产环境(linux下jar包运行读取)。
第一种:
ClassPathResource classPathResource = new ClassPathResource("excleTemplate/test.xlsx"); InputStream inputStream =classPathResource.getInputStream();
第二种:
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("excleTemplate/test.xlsx");
第三种:
InputStream inputStream = this.getClass().getResourceAsStream("/excleTemplate/test.xlsx");
第四种:
File file = ResourceUtils.getFile("classpath:excleTemplate/test.xlsx"); InputStream inputStream = new FileInputStream(file);
经测试:
前三种方法在开发环境(IDE中)和生产环境(linux部署成jar包)都可以读取到,第四种只有开发环境 时可以读取到,生产环境读取失败。
推测主要原因是springboot内置tomcat,打包后是一个jar包,无法直接读取jar包中的文件,读取只能通过类加载器读取。
前三种都可以读取到其实殊途同归,直接查看底层代码都是通过类加载器读取文件流,类加载器可以读取jar包中的编译后的class文件,当然也是可以读取jar包中的excle模板了。
用解压软件打开jar包查看结果如下:

其中cst文件中是编译后class文件存放位置,excleTemplate是模板存放位置,类加载器读取的是cst下class文件,同样可以读取excleTemplate下的模板的文件流了。
所以总结一下:假如文件是在jar包中,读取方式应当使用基于类加载器读取文件流的方式,比如前三种方法;使用基于java中File方式的读取,在jar包情况下是读取不到的,比方说第四种。
感觉对你有帮助,就点个赞吧……
作者:gigglesoso
链接:https://www.jianshu.com/p/7d7e5e4e8ae3
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
附录:
java读取resource文件,直接返回api
实际中遇到一个需求,返回地图的边界,数据量很大,而且不会变的,插入数据库很麻烦,于是我就存在了项目的resource文件夹下,直接读取文件返回,很方便的
@GetMapping("/home/mapArea") public BaseResponse<List> mapArea(@RequestParam String mapName) { String fileName = "xx.json"; try { ClassPathResource classPathResource = new ClassPathResource(fileName); InputStream inputStream = classPathResource.getInputStream(); String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8); JSONArray jsonArray = JSON.parseArray(text); return BaseResultUtils.success(jsonArray); } catch (IOException e) { e.printStackTrace(); } return null; }
附录:
新测试可用的读取resource目录下文件的方式
import com.google.common.base.Charsets; import com.google.common.io.Resources; public static String getWholeFile(String fileName) throws IOException { ClassPathResource classPathResource = new ClassPathResource(fileName); InputStream inputStream = classPathResource.getInputStream(); String line; StringBuilder sb = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { while ((line = br.readLine()) != null) { sb.append(line).append("\r\n"); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return ""; } public static String getFileContent(String fileName) throws IOException { URL url = Resources.getResource(fileName); String sdl = Resources.toString(url, Charsets.UTF_8); return sdl; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗