解决springboot读取jar包中文件的问题

转载自:

https://www.oschina.net/question/2272552_2269641

https://stackoverflow.com/questions/25869428/classpath-resource-not-found-when-running-as-jar

几个实现方式:

1
2
3
4
5
6
7
8
String data = “”;
ClassPathResource cpr = new ClassPathResource(“static/file.txt”);
try {
byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());
data = new String(bdata, StandardCharsets.UTF_8);
} catch (IOException e) {
LOG.warn(“IOException”, e);
}

场景二:

1
2
3
4
5
6
7
8
9
ClassPathResource classPathResource = new ClassPathResource(“static/something.txt”);

InputStream inputStream = classPathResource.getInputStream();
File somethingFile = File.createTempFile(“test”, “.txt”);
try {
FileUtils.copyInputStreamToFile(inputStream, somethingFile);
} finally {
IOUtils.closeQuietly(inputStream);
}

场景三:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Resource[] resources;

    try {
        resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:" + location + "/*.json");
        for (int i = 0; i < resources.length; i++) {
            try {
            InputStream is = resources[i].getInputStream();
            byte[] encoded = IOUtils.toByteArray(is);
            String content = new String(encoded, Charset.forName("UTF-8"));
            }
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }

转载自:

https://blog.csdn.net/zhuyu19911016520/article/details/79060389

在开发环境中,使用 ResourceUtils.getFile(“classpath:static/files/8k.wav”) 能读取到 File ,结果打包发布后,读取不了。

解决方法

1
2
3
InputStream stream = getClass().getClassLoader().getResourceAsStream(“static/files/8k.wav”);
File targetFile = new File(“files/8k.wav”);
FileUtils.copyInputStreamToFile(stream, targetFile);

或通过以下方式获取

1
2
3
ClassPathResource resource = new ClassPathResource(“static/office_template/word_replace_tpl.docx”);
File sourceFile = resource.getFile();
InputStream fis = resource.getInputStream();

参照此博客中的方法:https://blog.csdn.net/zhuyu19911016520/article/details/98071242

开发一个word替换功能时,因替换其中的内容功能需要 word 模版,就把 word_replace_tpl.docx 模版文件放到 resources 下

在开发环境中通过下面方法能读取word_replace_tpl.docx文件,但是打成jar包在 linux下运行后无法找到文件了

1
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + “static/office_template/xxx.docx”);
在开发环境运行时,会把资源文件编译到 项目\target\classes\static\office_template\xxx.docx 目录下,但是打包成jar后,
Resource下的文件是存在于jar这个文件里面,在磁盘上是没有真实路径存在的,它是位于jar内部的一个路径。所以通过ResourceUtils.getFile或者this.getClass().getResource("")方法无法正确获取文件。

我们用压缩软件打开 jar 文件,看看该word模版位于jar内部的路径

怎么解决

1.把该模版文件放到jar项目外,在项目中配置该模版文件的绝对路径,不太推荐这种方式,可能会忘记配置模版
2.通过 ClassPathResource resource = new ClassPathResource(“static/office_template/word_replace_tpl.docx”);方式读取

用第二种方式读取jar中的文件流

1
2
3
ClassPathResource resource = new ClassPathResource(“static/office_template/word_replace_tpl.docx”);
File sourceFile = resource.getFile();
InputStream fis = resource.getInputStream();
还要在项目pom.xml中配置resources情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23




src/main/java

/*.properties
/.xml
**/.yml


false


src/main/resources

**/.


false


再次发布项目,访问功能,测试后已经在服务器上能读取模版文件并生成出新文件了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值