解決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>

  

再次發布項目,訪問功能,測試后已經在服務器上能讀取模版文件並生成出新文件了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM