前言
SpringBoot微服務(wù)已成為業(yè)界主流,從開發(fā)到部署都非常省時(shí)省力,但是最近小明開發(fā)時(shí)遇到一個(gè)問題:在代碼中讀取資源文件(比如word文檔、導(dǎo)出模版等),本地開發(fā)時(shí)可以正常讀取 ,但是,當(dāng)我們打成jar包發(fā)布到服務(wù)器后,再次執(zhí)行程序時(shí)就會(huì)拋出找不到文件的異常。
背景
這個(gè)問題是在一次使用freemarker模版引擎導(dǎo)出word報(bào)告時(shí)發(fā)現(xiàn)的。大概說一下docx導(dǎo)出java實(shí)現(xiàn)思路:導(dǎo)出word的文檔格式為docx,事先準(zhǔn)備好一個(gè)排好版的docx文檔作為模版,讀取解析該模版,將其中的靜態(tài)資源替換再導(dǎo)出。
docx文檔本身其實(shí)是一個(gè)壓縮的zip文件,將其解壓過后就會(huì)發(fā)現(xiàn)它有自己的目錄結(jié)構(gòu)。
問題
這個(gè)docx文檔所在目錄如下圖所示:
在本地調(diào)試時(shí),我使用如下方式讀?。?/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" ); } |
可以正常解析使用,但是打包發(fā)布到beta環(huán)境卻不可用。拋出異常如下:
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 |
顯而易見,這個(gè)異常告訴我們:沒有找到文件,但是將jar包解壓過后,發(fā)現(xiàn)這個(gè)文件是真真實(shí)實(shí)存在的。
那這到底是怎么回事呢?這壓根難不倒我。我們要善于透過堆棧信息看本質(zhì)。通過仔細(xì)觀察堆棧信息,我發(fā)現(xiàn)此時(shí)的文件路徑并不是一個(gè)合法的URL(文件資源定位符)。原來jar包中資源有其專門的URL形式: jar:<url>!/{entry} )。所以,此時(shí)如果仍然按照標(biāo)準(zhǔn)的文件資源定位形式
1
|
File f= new File( "jar:file:……" ); |
定位文件,就會(huì)拋出java.io.FileNotFoundException。
解決
雖然我們不能用常規(guī)操作文件的方法來讀取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" ); } |
拿到流之后,就可以將其轉(zhuǎn)換為任意一個(gè)我們需要的對(duì)象,比如File、String等等,此處我要獲取docxTemplate.docx下的目錄結(jié)構(gòu),因此我需要一個(gè)File對(duì)象,代碼舉例如下:
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的工具類即可轉(zhuǎn)換 FileUtils.copyToFile(inputStream,docxFile); ZipFile zipFile = new ZipFile(docxFile); Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries(); // todo 記得關(guān)閉流 } |
結(jié)果
打包、發(fā)布至beta環(huán)境,親測(cè)可用,問題完美解決。
到此這篇關(guān)于解決SpringBoot jar包中的文件讀取問題實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot jar包文件讀取內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://segmentfault.com/a/1190000023777147