Spring项目读取resource下的文件
目录
一、前提条件
2.1、Controller、service中使用ClassPathResource
一、前提条件
要去读取的文件是存放在project/src/main/resources目录下的,如下图中的test.txt文件。

二、使用ClassPathResource类读取
2.1、Controller、service中使用ClassPathResource
不管是在哪一层(service、controller..),都可以使用这种方式,甚至是单元测试中,也是可以的。
package cn.ganlixin.demo.controller; import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException; @RestController
public class TestController { @RequestMapping("testFile")
public String testFile() throws IOException {
// ClassPathResource类的构造方法接收路径名称,自动去classpath路径下找文件
ClassPathResource classPathResource = new ClassPathResource("test.txt"); // 获得File对象,当然也可以获取输入流对象
File file = classPathResource.getFile(); BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
StringBuilder content = new StringBuilder();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
content.append(line);
} return content.toString();
}
}
2.2、单元测试使用ClassPathResource
单元测试也是可以使用ClassPathResource,但是需要注意:
1、单元测试的资源目录默认是project/src/test/resources,如果该目录下找到单元测试需要的文件,那么就用找到的文件;
2、如果在单元测试的资源目录下没有找到单元测试需要的文件,就会去找/project/src/main/resources目录下的同名文件进行操作。
package cn.ganlixin.demo.example; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException; @RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationConfigTest { @Test
public void testFile() throws IOException {
final ClassPathResource classPathResource = new ClassPathResource("test.txt");
final File file = classPathResource.getFile(); final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
}
三、使用FileSystemResource类读取文件
FileSystemResource这个类在找文件的时候就是按照给定的路径名去找,默认的当前目录就是项目根目录。
使用该类来查找文件时,需要保证文件路径完全正确,另外,在代码中将路径写死是一个不好的习惯,特别是一个文件的路径在不同的主机上的位置(层级目录)不一定相同,所以我们开发过程中很少使用这种方式。
FileSystemResource的用法和ClassPathResource的用法相似,因为他们都继承了AbstractResource这个抽象类。
package cn.ganlixin.demo.example; import org.junit.Test;
import org.springframework.core.io.FileSystemResource; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException; public class FileTest extends ApplicationConfigTest { @Test
public void testFile() throws IOException { FileSystemResource resource = new FileSystemResource("./");
System.out.println(resource.getFile().getAbsolutePath());
// 传入当前路径,获得的是项目根目录:/Users/ganlixin/code/Spring/demo/example/. // 传入根目录路径,获得的就是操作系统的根目录
resource = new FileSystemResource("/");
System.out.println(resource.getFile().getAbsolutePath()); // 输出 / // 获取单元测试resources目录下的test.txt,需要指定详细的路径
resource = new FileSystemResource("src/test/resources/test.txt");
final File file = resource.getFile(); final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
}
这里就列举了两种方式,还有其他很多方式,这两种应该够用了
Spring项目读取resource下的文件的更多相关文章
- SpringBoot项目构建成jar运行后,如何正确读取resource下的文件
SpringBoot项目构建成jar运行后,如何正确读取resource下的文件 不管你使用的是SpringBoot 1.x还是SpringBoot2.x,在开Dev环境中使用eclipse.IEAD ...
- springboot项目获取resource下的文件
package com.expr.exceldemo; import org.springframework.core.io.ClassPathResource; public class Test ...
- Spring Boot 读取 resource 下文件
支持linux下读取 import org.springframework.core.io.ClassPathResource; public byte[] getCertStream(String ...
- Springboot项目读取resource下的静态资源方法
如果按相对路径直接读会定位到target下,因为springboot打包后读到这里 如果做单元测试的话是找不到文件的 File jsonFile = ResourceUtils.getFile(&qu ...
- Maven 工程读取resource下的文件
1:方式1: public static List<String> userList; static { userList = new LinkedList<String>() ...
- springboot读取resource下的文件
public static String DEFAULT_CFGFILE = ConfigManager.class.getClassLoader().getResource("conf/s ...
- springboot 读取 resource 下的文件
ClassPathResource classPathResource = new ClassPathResource("template/demo/200000168-check-resp ...
- SpringBoot读取Resource下文件的几种方式(十五)
需求:提供接口下载resources目录下的模板文件,(或者读取resources下的文件)给后续批量导入数据提供模板文件. 方式一:ClassPathResource //获取模板文件:注意此处需要 ...
- maven工程读取resource下配置文件
maven工程读取resource下配置文件 在maven工程中,我们会将配置文件放到,src/main/resources 下面,例如 我们需要确认resource 下的文件 编译之后存放的位置 ...
随机推荐
- JanusGraph安装graphexp
准备:JanusGraph环境,graphexp源码,nginx 本文采用的环境:JanusGraph + cassandra + ES + GraphExp(cassandra 或者HBase作为后 ...
- 性能测试基础---联机负载&IP欺骗
·联机负载&IP欺骗 ·联机负载:又叫分布式负载,即通过多台负载机(压力机)运行脚本,向服务器发送请求,从而实现更多的负载压力. ·联机负载的具体操作: ·了解两个概念: ·控制机:所谓控制机 ...
- 警告:Establishing SSL connection without server’s identity verification is not recommended
SpringBoot启东时红色警告: Mon Jun 04 00:53:48 CST 2018 WARN: Establishing SSL connection without server's i ...
- JVM 性能调优工具
jdk自带的工具,在macOs系统中的目录位置(jdk具体版本位置要替换):/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Ho ...
- ASP.NET 内容管理系统CMS
一.Umbraco 项目地址: http://umbraco.org/ Umbraco是一个开放源码的CMS内容管理系统,基于asp.net建立,使用mssql进行存储数据. 使用Umbraco ,设 ...
- 平衡二叉树(Java)
package com.rao.linkList; /** * @author Srao * @className AvlTree * @date 2019/12/3 21:23 * @package ...
- Numpy | 03 数据类型
numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型. 下表列举了常用 NumPy 基本类型: 名称 描 ...
- 复旦高等代数I(19级)每周一题
本学期的高等代数每周一题活动计划从第2教学周开始,到第15教学周结束,每周的周末公布一道思考题(共14道,思考题一般与下周授课内容密切相关),供大家思考和解答.每周一题将通过“高等代数官方博客”(以博 ...
- WeUI框架
WeUI框架 WeUI是一套小程序的UI框架,所谓UI框架就是一套界面设计方案,有了组件,我们可以用它来拼接出一个内容丰富的小程序,而有了UI框架,我们就可以让我们的小程序变得更加美观. 体验WeUi ...
- jstl的if标签和forEach标签的解析
今天上午学习了jstl的if标签和forEach标签(其它标签用的很少,所以没讲,只讲了这两个标签),然后通过代码练习了一下,现在总结. 首先导入包,从Apache的网站下载JSTL的JAR包.进入 ...