读取项目中的json文件遇到的文件找不到异常

1 篇文章 0 订阅

读取项目中的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包运行起来
在这里插入图片描述

测试的结果:

测试test01,使用流的方式完全没有问题.

在这里插入图片描述

测试test02,使用的原来的方式,依然报错.但是本地可以.

在这里插入图片描述

测试test03,使用的是复制一下流到文件的方式,测试是没有问题的

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值