经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
springboot读取resources下文件的方式详解
来源:jb51  时间:2022/6/21 10:20:23  对本文有异议

项目中很多时候需要读取自定义配置文件,本地开发工具怎么写都成功但是部署到服务其上就出现问题,

异常BOOT-INF/classes!/config.xml (文件名、目录名或卷标语法不正确.)路径中带有叹号之类的

了解了大概之后就是springboot打成jar是一个文件,也就是一个压缩包,没有办法读取压缩文件里的路径,因此要解决这个问题了解读取配置文件的原理,直接获取文件流就可以了。

1、使用项目内路径读取,只能在开发工具中使用,部署之后无法读取。(不通用

类似:src/main/resources/default.xml

File file = new File("src/main/resources/default.xml");

  1. @Test
  2. public void testReadFile2() throws IOException {
  3. File file = new File("src/main/resources/default.xml");
  4. FileInputStream fis = new FileInputStream(file);
  5. InputStreamReader isr = new InputStreamReader(fis);
  6. BufferedReader br = new BufferedReader(isr);
  7. String data = null;
  8. while((data = br.readLine()) != null) {
  9. System.out.println(data);
  10. }
  11. br.close();
  12. isr.close();
  13. fis.close();
  14. }

 2、使用org.springframework.util.ResourceUtils,读取。在linux环境中无法读取。(不通用)

File file = ResourceUtils.getFile("classpath:default.xml");
FileInputStream fis = new FileInputStream(file);

  1. @Test
  2. public void testReadFile3() throws IOException {
  3. File file = ResourceUtils.getFile("classpath:default.xml");
  4. FileInputStream fis = new FileInputStream(file);
  5. InputStreamReader isr = new InputStreamReader(fis);
  6. BufferedReader br = new BufferedReader(isr);
  7. String data = null;
  8. while((data = br.readLine()) != null) {
  9. System.out.println(data);
  10. }
  11. br.close();
  12. isr.close();
  13. fis.close();
  14. }

3、使用org.springframework.core.io.ClassPathResource,各种环境都能读取。(通用)

Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();

  1. @Test
  2. public void testReadFile() throws IOException {
  3. // ClassPathResource classPathResource = new ClassPathResource("default.xml");
  4. Resource resource = new ClassPathResource("default.xml");
  5. InputStream is = resource.getInputStream();
  6. InputStreamReader isr = new InputStreamReader(is);
  7. BufferedReader br = new BufferedReader(isr);
  8. String data = null;
  9. while((data = br.readLine()) != null) {
  10. System.out.println(data);
  11. }
  12. br.close();
  13. isr.close();
  14. is.close();
  15. }

4、结合spring注解,使用org.springframework.core.io.ResourceLoader;类的注解。(通用)

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.core.io.Resource;
  10. import org.springframework.core.io.ResourceLoader;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest
  14. public class ApplicationTests {
  15. @Autowired
  16. ResourceLoader resourceLoader;
  17. @Test
  18. public void testReaderFile() throws IOException {
  19. Resource resource = resourceLoader.getResource("classpath:default.xml");
  20. InputStream is = resource.getInputStream();
  21. InputStreamReader isr = new InputStreamReader(is);
  22. BufferedReader br = new BufferedReader(isr);
  23. String data = null;
  24. while((data = br.readLine()) != null) {
  25. System.out.println(data);
  26. }
  27. br.close();
  28. isr.close();
  29. is.close();
  30. }
  31. }

总结

到此这篇关于springboot读取resources下文件的文章就介绍到这了,更多相关springboot读取resources文件内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号