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 下的文件 编译之后存放的位置 ...
随机推荐
- kuangbin专题专题四 Currency Exchange POJ - 1860
题目链接:https://vjudge.net/problem/POJ-1860 大致题意:有不同的货币,有很多货币交换点,每个货币交换点只能两种货币相互交换,有佣金C,汇率R. 每次交换算一次操作, ...
- 【使用DIV+CSS重写网站首页案例】CSS浮动
CSS浮动: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边缘为止 由于浮动框不在文件的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样. 选择器之 float属性 ...
- Jmeter中while循环逻辑控制器+配置元件计数器的用法
一.在线程组下添加逻辑控制器While Controller 二.在逻辑控制器While Controller下添加Sample,BeanShell Sampler , 三.逻辑控制器While Co ...
- ovirt磁盘类型(IDE, virtio, virtio-scsi)
ovirt磁盘类型辨析(IDE, virtio, virtio-scsi) 通过一张表格,简单明了的说明这三种硬盘的不同: 整体上来看这三者的最大不同还是挂载磁盘的数量.根据在ovirt的上测试,一台 ...
- A A=new A();
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- PureComponent & shouldComponentUpdate
Called to determine whether the change in props and state should trigger a re-render. Component alwa ...
- 使用 ML.NET 对 K-Means 平均值聚类分析和分类
数据集 :https://en.wikipedia.org/wiki/Iris_flower_data_set 聚类分析 非监管式机器学习任务,用于将数据实例分组到包含类似特性的群集. 聚类分析还可用 ...
- 解决mysql无法显示中文/MySQL中文乱码问号等问题
一般都是编码格式问题 显示编码格式: show variables like'character_set_%'; 将其中Value不是utf8的改为utf8: set character_set_cl ...
- nginx 访问控制之 document_uri
这就用到了变量$document_uri,根据前面所学内容,该变量等价于$uri,其实也等价于location匹配. 示例1: if ($document_uri ~ "/admin/&qu ...
- django 下载文件,指定文件中文名称
Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件.Content-disposition其实可以控制用户请求所得的内容存为一个 ...