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

转载自:

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[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 ,结果打包发布后,读取不了。

解决方法

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>

  

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

 

posted @ 2019-08-26 10:35  不知名的宋某某  阅读(16560)  评论(0编辑  收藏  举报