前言
SpringBoot微服務已成為業界主流,從開發到部署都非常省時省力,但是最近小明開發時遇到一個問題:在代碼中讀取資源文件(比如word文檔、導出模版等),本地開發時可以正常讀取 ,但是,當我們打成jar包發布到服務器后,再次執行程序時就會拋出找不到文件的異常。
背景
這個問題是在一次使用freemarker模版引擎導出word報告時發現的。大概說一下docx導出java實現思路:導出word的文檔格式為docx,事先準備好一個排好版的docx文檔作為模版,讀取解析該模版,將其中的靜態資源替換再導出。
docx文檔本身其實是一個壓縮的zip文件,將其解壓過后就會發現它有自己的目錄結構。
問題
這個docx文檔所在目錄如下圖所示:
在本地調試時,我使用如下方式讀?。?/p>
1
2
3
4
|
import org.springframework.util.ResourceUtils; public static void main(String[] args) throws IOException { File docxTemplate = ResourceUtils.getFile( "classpath:templates/docxTemplate.docx" ); } |
可以正常解析使用,但是打包發布到beta環境卻不可用。拋出異常如下:
1
|
java.io.FileNotFoundException: class path resource [templates/docxTemplate.docx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/usr/local/subject-server.jar!/BOOT-INF/classes!/templates/docxTemplate.docx |
顯而易見,這個異常告訴我們:沒有找到文件,但是將jar包解壓過后,發現這個文件是真真實實存在的。
那這到底是怎么回事呢?這壓根難不倒我。我們要善于透過堆棧信息看本質。通過仔細觀察堆棧信息,我發現此時的文件路徑并不是一個合法的URL(文件資源定位符)。原來jar包中資源有其專門的URL形式: jar:<url>!/{entry} )。所以,此時如果仍然按照標準的文件資源定位形式
1
|
File f= new File( "jar:file:……" ); |
定位文件,就會拋出java.io.FileNotFoundException。
解決
雖然我們不能用常規操作文件的方法來讀取jar包中的資源文件docxTemplate.docx,但可以通過Class類的getResourceAsStream()方法,即通過流的方式來獲取 :
1
2
3
|
public static void main(String[] args) throws IOException { InputStream inputStream = WordUtil. class .getClassLoader().getResourceAsStream( "templates/docxTemplate.docx" ); } |
拿到流之后,就可以將其轉換為任意一個我們需要的對象,比如File、String等等,此處我要獲取docxTemplate.docx下的目錄結構,因此我需要一個File對象,代碼舉例如下:
1
2
3
4
5
6
7
8
9
10
11
|
import org.apache.commons.io.FileUtils; public static void main(String[] args) throws IOException { InputStream inputStream = WordUtil. class .getClassLoader().getResourceAsStream( "templates/docxTemplate.docx" ); File docxFile = new File( "docxTemplate.docx" ); // 使用common-io的工具類即可轉換 FileUtils.copyToFile(inputStream,docxFile); ZipFile zipFile = new ZipFile(docxFile); Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries(); // todo 記得關閉流 } |
結果
打包、發布至beta環境,親測可用,問題完美解決。
到此這篇關于解決SpringBoot jar包中的文件讀取問題實現的文章就介紹到這了,更多相關SpringBoot jar包文件讀取內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://segmentfault.com/a/1190000023777147