spring 读取jar包内部的静态文件失败

本文描述了在SpringBoot项目中,从classPath读取文件遇到的问题及解决方案。在本地运行正常的情况下,打包成jar后出现文件读取失败。通过使用ClassPathResource的getInputStream()方法替代getFile()方法,成功解决了文件读取问题。

问题描述:最近打包项目发现,本地正常执行的代码,打成jar包执行会出现文件读取失败的问题。

package com.liu.springboot.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
/**
*将static中的favicon.ico 复制到D盘
*
*/
@Controller
public class TestFile {

    @RequestMapping("/getFile")
    @ResponseBody
    public String read() {
        ClassPathResource classPathResource = new ClassPathResource("static/favicon.ico");

        File copy = new File("D://copy.ico");
        OutputStream outputStream = null;
        InputStream inputStream = null;
        int len;
        byte[] buf = new byte[100];
        try {
            outputStream = new FileOutputStream(copy);
            File file = classPathResource.getFile();
            inputStream = new FileInputStream(file);
            while ((len = inputStream.read(buf))>0) {
                outputStream.write(buf,0,len);
            }
//            System.out.println("ok");
            return  "ok";
        }catch (IOException io) {
            return  "false";
        } finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

在idea中执行是ok的
可是打包后就会出现异常
在这里插入图片描述
发现这行代码出现问题,报了空指针

  File file = classPathResource.getFile();

解决办法:

 outputStream = new FileOutputStream(copy);
//File file = classPathResource.getFile();
//inputStream = new FileInputStream(file);
//直接得到输入流
 inputStream = classPathResource.getInputStream();
 while ((len = inputStream.read(buf))>0) {
     outputStream.write(buf,0,len);
 }

这样就可以了,其实发现完全没必要使用classPathResource.getFile(),直接 classPathResource.getInputStream();就行了。
如果你在某种情况下实在需要以File形式,那么你可以将从 classPathResource.getInputStream();获得的流里的内容,复制一份到文件中,也就是上文做的工作·,然后再操作(读)这个文件的拷贝文件。

暂时记录下,以后再详细查找原因

Spring Cloud中,我们可以使用Spring Framework提供的ResourceLoader接口来读取静态文件。 首先,我们需要在Spring Boot应用的classpath下创建一个名为"resources"的文件夹,将静态文件(如文本文件、配置文件等)放置在其中。 然后,在我们的Spring Boot应用中,我们可以通过注入ResourceLoader接口来实现对静态文件读取。例如,在一个Controller类中,我们可以如下定义一个方法来读取静态文件: ```java @Autowired private ResourceLoader resourceLoader; @GetMapping("/readFile") public String readFile() throws IOException { Resource resource = resourceLoader.getResource("classpath:static/myFile.txt"); File file = resource.getFile(); // 执行文件读取操作,例如: try (BufferedReader reader = new BufferedReader(new FileReader(file))) { String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { stringBuilder.append(line); } return stringBuilder.toString(); } } ``` 在上述代码中,"classpath:static/myFile.txt"指定了要读取静态文件的路径。getResource()方法将该路径解析为Resource对象,然后我们可以通过getFile()方法获取该文件的File对象。接下来,我们可以利用File对象执行文件读取操作,读取文件的内容并返回。 需要注意的是,如果静态文件位于jar内部,则无法通过getFile()方法获取文件的File对象。此时,我们可以使用其他方法,如使用InputStream进行读取,如下所示: ```java @Autowired private ResourceLoader resourceLoader; @GetMapping("/readFile") public String readFile() throws IOException { Resource resource = resourceLoader.getResource("classpath:static/myFile.txt"); InputStream inputStream = resource.getInputStream(); // 执行文件读取操作,例如: try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; StringBuilder stringBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { stringBuilder.append(line); } return stringBuilder.toString(); } } ``` 以上示例演示了如何读取静态文件,无论文件是放置在classpath下还是在jar内部。使用ResourceLoader接口,我们可以很方便地读取静态文件并执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值