读取项目中的json文件遇到的文件找不到异常
异常
在读取项目中的json文件时碰到的异常;
/* 异常:
* class path resource [data/user.json]
* cannot be resolved to absolute file path
* because it does not reside in the file system:
* jar:file:/F:/02_testautowire-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/data/user.json
* */
错误解析的方法
使用的ClassPathResource 的getFile()方法,方法提示在jar包中的user.json找不到,但是在本地测试没有问题.
/**
* 如果执意要使用File类来读取的话,本地没有用问题,
* 但是打成jar包在线上就会出现问题,提示无法读取
*/
public static User useFileFalse() throws IOException {
/* 异常:
* class path resource [data/user.json]
* cannot be resolved to absolute file path
* because it does not reside in the file system:
* jar:file:/F:/02_testautowire-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/data/user.json
* */
ClassPathResource resource = new ClassPathResource("data/user.json");
if (!resource.exists()) {
System.out.println("资源找不到");
return null;
}
//取得文件这个方法在本地可行,在线上是取不到的
File file = resource.getFile();
System.out.println(file.getPath());
//本地是true,打成jar包是false
System.out.println(file.exists());
InputStream inputStream = new FileInputStream(file);
User user = JSON.parseObject(inputStream, StandardCharsets.UTF_8, User.class);
System.out.println("========" + user);
return user;
}
解决方法
其中使用到的pom依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<!--Apache的工具包,有对文件操作相关的封装-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
创建JSONUtil工具类
/**
* 读取根目录下的json文件,使用流是可以成功的,无论是在本地还是在线上
*/
public static User useInputStream() throws IOException {
ClassPathResource resource = new ClassPathResource("data/user.json");
if (!resource.exists()) {
System.out.println("资源找不到");
return null;
}
//获取文件输入流
InputStream inputStream = resource.getInputStream();
//这里可以使用fastjson来将流装换为实体类.(可以配置是否支持单引号的配置)
User user = JSON.parseObject(inputStream, StandardCharsets.UTF_8, User.class);
inputStream.close();
System.out.println("===================" + user.toString());
return user;
}
/**
* 如果执意要使用File类来读取的话,本地没有用问题,
* 但是打成jar包在线上就会出现问题,提示无法读取
*/
public static User useFileFalse() throws IOException {
/* 异常:
* class path resource [data/user.json]
* cannot be resolved to absolute file path
* because it does not reside in the file system:
* jar:file:/F:/02_testautowire-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/data/user.json
* */
ClassPathResource resource = new ClassPathResource("data/user.json");
if (!resource.exists()) {
System.out.println("资源找不到");
return null;
}
//取得文件这个方法在本地可行,在线上是取不到的
File file = resource.getFile();
System.out.println(file.getPath());
//本地是true,打成jar包是false
System.out.println(file.exists());
InputStream inputStream = new FileInputStream(file);
User user = JSON.parseObject(inputStream, StandardCharsets.UTF_8, User.class);
System.out.println("========" + user);
return user;
}
/**
* 假如是调用的封装的方法,就需要使用File,怎么办?
* 将流复制一下,新建一个文件到一个固定的路径也行
*/
public static User useFileTrue() throws IOException {
ClassPathResource resource = new ClassPathResource("data/user.json");
if (!resource.exists()) {
System.out.println("资源找不到");
return null;
}
//先获取输入流
InputStream inputStream = resource.getInputStream();
File file = new File("data/user.json");
//然后拷贝到指定的文件中,使用的commons-io包中的工具类
FileUtils.copyInputStreamToFile(inputStream, file);
//这的file无论jar包里还是本地就都存在了
System.out.println(file.exists());
//然后在使用就可以了
InputStream in = new FileInputStream(file);
User user = JSON.parseObject(in, StandardCharsets.UTF_8, User.class);
System.out.println("========" + user);
return user;
}
}
测试一下
将工具类使用Controller封装一下
@RestController
public class TestController {
@RequestMapping("/test01")
public Object useInputStream() throws IOException {
return JSONUtil.useInputStream();
}
@RequestMapping("/test02")
public Object useFileFalse() throws IOException {
return JSONUtil.useFileFalse();
}
@RequestMapping("/test03")
public Object useFileTrue() throws IOException {
return JSONUtil.useFileTrue();
}
}
发布jar包
将jar包运行起来