SpringBoot項(xiàng)目打包成jar后獲取classpath下文件失敗的解決
前言
公司的一個(gè)SpringBoot項(xiàng)目中,有需要下載文件模板的需求,按理來說分布式項(xiàng)目文件都應(yīng)該上傳到文件服務(wù)器,但是由于文件不是太多于是就放在了classpath下,在本地開發(fā)的時(shí)候發(fā)現(xiàn)都能正常下載文件,但是打包成jar上傳到Linxu測(cè)試環(huán)境上就報(bào)錯(cuò),找不到classpath路徑。
原因
原因是項(xiàng)目打包后Spring試圖訪問文件系統(tǒng)路徑,但無法訪問JAR包中的路徑。
我們使用
ResourceUtils.getFile("classpath:");
這樣的方式是獲取不到路徑的。
解決方案
我們雖然不能直接獲取文件資源路徑,但是我們可以通過流的方式讀取資源,拿到輸入流過后我們就可以對(duì)其做操作了。
關(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 { //輸出流自動(dòng)關(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. 測(cè)試
使用Maven工具把項(xiàng)目打成jar包
在target下生成了jar包
進(jìn)入jar包所在的文件夾,按住shift并右擊,點(diǎn)擊在此處打開命令行窗口。
輸入命令啟動(dòng)項(xiàng)目 java -jar 打包后的文件
我設(shè)置的端口是8086,瀏覽器地址欄輸入http://127.0.0.1:8086/download/pattern
此時(shí)我們可以卡看到test.txt文件下載成功
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題
這篇文章主要介紹了關(guān)于HttpClient 引發(fā)的線程太多導(dǎo)致FullGc的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項(xiàng)目”功能
這篇文章主要介紹了IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項(xiàng)目”功能,本文給大家分享了idea2020.3.3激活碼的詳細(xì)破解教程,每種方法都很好用,使用idea2020.3以下所有版本,需要的朋友可以參考下2021-03-03springboot執(zhí)行延時(shí)任務(wù)之DelayQueue實(shí)例
這篇文章主要介紹了springboot執(zhí)行延時(shí)任務(wù)之DelayQueue實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02SpringBoot結(jié)合mybatis-plus實(shí)現(xiàn)分頁的項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot結(jié)合mybatis-plus實(shí)現(xiàn)分頁的項(xiàng)目實(shí)踐,主要基于MyBatis-Plus 自帶的分頁插件 PaginationInterceptor,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06深入解析Spring Cloud內(nèi)置的Zuul過濾器
這篇文章主要給大家深入的介紹了Spring Cloud內(nèi)置的Zuul過濾器的相關(guān)資料,文中給大家介紹的很詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-02-02