解決java項(xiàng)目jar打包后讀取文件失敗的問題
java項(xiàng)目jar打包后讀取文件失敗
在本地項(xiàng)目讀取文件時(shí)
this.getClass().getClassLoader().getResource("").getPath()+fileName this.getClass().getResource("/filename").getPath()
都是可以成功的
但是jar打包后上面方式讀取文件時(shí) 會變成 jar!filename 這樣的形式去讀取文件,這樣是讀取不到文件的
可以使用
Test.class.getResourceAsStream("/filename")
讀取文件 以流的形式讀取文件 是可以讀取的到的
這樣就可以在打包后將文件進(jìn)行讀取
SpringBoot項(xiàng)目打包成jar后獲取classpath下文件失敗
公司的一個(gè)SpringBoot項(xiàng)目中,有需要下載文件模板的需求,按理來說分布式項(xiàng)目文件都應(yīng)該上傳到文件服務(wù)器,但是由于文件不是太多于是就放在了classpath下,在本地開發(fā)的時(shí)候發(fā)現(xiàn)都能正常下載文件,但是打包成jar上傳到Linxu測試環(huán)境上就報(bào)錯(cuò),找不到classpath路徑。
原因
原因是項(xiàng)目打包后Spring試圖訪問文件系統(tǒng)路徑,但無法訪問JAR包中的路徑。
我們使用ResourceUtils.getFile("classpath:");這樣的方式是獲取不到路徑的。
解決方案
我們雖然不能直接獲取文件資源路徑,但是我們可以通過流的方式讀取資源,拿到輸入流過后我們就可以對其做操作了。
關(guān)鍵代碼如下:
ClassPathResource resource = new ClassPathResource("\\static\\pattern\\test.txt"); // static/pattern下的 test.txt文件 InputStream in = resource.getInputStream(); //獲取文件輸入流
示例Demo
1. 在static下新建pattern目錄,并新建一個(gè)名為 test.txt的文件
2. 新建DownloadController.java
代碼如下:
package com.example.jekins.controller; import org.springframework.core.io.ClassPathResource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; @RestController public class DownloadController { @GetMapping("/download/pattern") public void downloadPattern(HttpServletRequest request, HttpServletResponse response){ System.out.println("開始下載文件....."); ClassPathResource resource = new ClassPathResource("\\static\\pattern\\test.txt"); try { //獲取文件輸入流 InputStream in = resource.getInputStream(); //下載文件 downFile("test文件.txt",request,response,in); } catch (IOException e) { e.printStackTrace(); } } /** * 下載文件 * @param fileName 下載文件名稱 * @param response 響應(yīng) * @throws IOException 異常 */ public static void downFile(String fileName,HttpServletRequest request, HttpServletResponse response,InputStream in) throws IOException { //輸出流自動關(guān)閉,java1.7新特性 try(OutputStream os = response.getOutputStream()) { fileName = URLEncoder.encode(fileName, "UTF-8"); response.reset(); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); response.setContentType("application/octet-stream; charset=UTF-8"); byte[] b = new byte[in.available()]; in.read(b); os.write(b); os.flush(); } catch (Exception e) { System.out.println("fileName=" + fileName); e.printStackTrace(); } finally { if (in != null) { in.close(); } } } }
3. 測試
使用Maven工具把項(xiàng)目打成jar包
在target下生成了jar包
進(jìn)入jar包所在的文件夾,按住shift并右擊,點(diǎn)擊在此處打開命令行窗口。輸入命令啟動項(xiàng)目 java -jar 打包后的文件
我設(shè)置的端口是8086,瀏覽器地址欄輸入http://127.0.0.1:8086/download/pattern
此時(shí)我們可以卡看到test.txt文件下載成功
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在Eclipse IDE使用Gradle構(gòu)建應(yīng)用程序(圖文)
這篇文章主要介紹了在Eclipse IDE使用Gradle構(gòu)建應(yīng)用程序(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Java Math類的三個(gè)方法ceil,floor,round用法
這篇文章主要介紹了Java Math類的三個(gè)方法ceil,floor,round用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java使用RedisTemplate操作Redis遇到的坑
這篇文章主要介紹了Java使用RedisTemplate操作Redis遇到的坑,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12MyBatis-Plus集成Druid環(huán)境搭建的詳細(xì)教程
這篇文章主要介紹了MyBatis-Plus集成Druid環(huán)境搭建的詳細(xì)教程,需要的朋友可以參考下2020-08-08詳解如何使用MongoDB+Springboot實(shí)現(xiàn)分布式ID的方法
這篇文章主要介紹了詳解如何使用MongoDB+Springboot實(shí)現(xiàn)分布式ID的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09