Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 11385|回复: 0

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

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-4-4 16:45:27 | 显示全部楼层 |阅读模式

    转载自:

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

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

     

    几个实现方式:

    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);
    }
    

      

    场景二:

    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);
    }
    

      

    场景三:

    Resource[] resources;
    
            try {
                resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader).getResources("classpath:" + location + "/*.json");
                for (int i = 0; i < resources.length; i++) {
                    try {
                    InputStream is = resources.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 ,结果打包发布后,读取不了。

    解决方法

    InputStream stream = getClass().getClassLoader().getResourceAsStream("static/files/8k.wav");
    File targetFile = new File("files/8k.wav");
    FileUtils.copyInputStreamToFile(stream, targetFile);
    

     

    或通过以下方式获取

    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下运行后无法找到文件了

    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中的文件流

    ClassPathResource resource = new ClassPathResource("static/office_template/word_replace_tpl.docx");
    File sourceFile = resource.getFile();
    InputStream fis = resource.getInputStream();
    

    还要在项目pom.xml中配置resources情况

    <build>
    	<!-- 定义包含这些资源文件,能在jar包中获取这些文件 -->
    	<resources>
    		<resource>
    			<directory>src/main/java</directory>
    			<includes>
    				<include>**/*.properties</include>
    				<include>**/*.xml</include>
    				<include>**/*.yml</include>
    			</includes>
    			<!--是否替换资源中的属性-->
    			<filtering>false</filtering>
    		</resource>
    		<resource>
    			<directory>src/main/resources</directory>
    			<includes>
    				<include>**/*.*</include>
    			</includes>
    			<!--是否替换资源中的属性-->
    			<filtering>false</filtering>
    		</resource>
    	</resources>
    </build>
    

      

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

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-1 02:53 , Processed in 0.074997 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表