解決java項目jar打包后讀取文件失敗的問題
java項目jar打包后讀取文件失敗
在本地項目讀取文件時
this.getClass().getClassLoader().getResource("").getPath()+fileName this.getClass().getResource("/filename").getPath()
都是可以成功的
但是jar打包后上面方式讀取文件時 會變成 jar!filename 這樣的形式去讀取文件,這樣是讀取不到文件的
可以使用
Test.class.getResourceAsStream("/filename")
讀取文件 以流的形式讀取文件 是可以讀取的到的
這樣就可以在打包后將文件進行讀取
SpringBoot項目打包成jar后獲取classpath下文件失敗
公司的一個SpringBoot項目中,有需要下載文件模板的需求,按理來說分布式項目文件都應(yīng)該上傳到文件服務(wù)器,但是由于文件不是太多于是就放在了classpath下,在本地開發(fā)的時候發(fā)現(xiàn)都能正常下載文件,但是打包成jar上傳到Linxu測試環(huán)境上就報錯,找不到classpath路徑。
原因
原因是項目打包后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目錄,并新建一個名為 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工具把項目打成jar包
在target下生成了jar包
進入jar包所在的文件夾,按住shift并右擊,點擊在此處打開命令行窗口。輸入命令啟動項目 java -jar 打包后的文件
我設(shè)置的端口是8086,瀏覽器地址欄輸入http://127.0.0.1:8086/download/pattern
此時我們可以卡看到test.txt文件下載成功
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java concurrency之集合_動力節(jié)點Java學院整理
Java集合主體內(nèi)容包括Collection集合和Map類;而Collection集合又可以劃分為List(隊列)和Set(集合),有需要的小伙伴可以參考下2017-06-06Springboot如何使用@Async實現(xiàn)異步任務(wù)
這篇文章主要介紹了Springboot如何使用@Async實現(xiàn)異步任務(wù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09java編程之基于SpringBoot框架實現(xiàn)掃碼登錄
本文將介紹基于SpringBoot + Vue + Android實現(xiàn)的掃碼登錄demo的總體思路,文中附含詳細示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09