解決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項目中,有需要下載文件模板的需求,按理來說分布式項目文件都應該上傳到文件服務器,但是由于文件不是太多于是就放在了classpath下,在本地開發(fā)的時候發(fā)現(xiàn)都能正常下載文件,但是打包成jar上傳到Linxu測試環(huán)境上就報錯,找不到classpath路徑。
原因
原因是項目打包后Spring試圖訪問文件系統(tǒng)路徑,但無法訪問JAR包中的路徑。
我們使用ResourceUtils.getFile("classpath:");這樣的方式是獲取不到路徑的。
解決方案
我們雖然不能直接獲取文件資源路徑,但是我們可以通過流的方式讀取資源,拿到輸入流過后我們就可以對其做操作了。
關鍵代碼如下:
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 響應
* @throws IOException 異常
*/
public static void downFile(String fileName,HttpServletRequest request,
HttpServletResponse response,InputStream in) throws IOException {
//輸出流自動關閉,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 打包后的文件

我設置的端口是8086,瀏覽器地址欄輸入http://127.0.0.1:8086/download/pattern
此時我們可以卡看到test.txt文件下載成功

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在Eclipse IDE使用Gradle構建應用程序(圖文)
這篇文章主要介紹了在Eclipse IDE使用Gradle構建應用程序(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Java Math類的三個方法ceil,floor,round用法
這篇文章主要介紹了Java Math類的三個方法ceil,floor,round用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java使用RedisTemplate操作Redis遇到的坑
這篇文章主要介紹了Java使用RedisTemplate操作Redis遇到的坑,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
MyBatis-Plus集成Druid環(huán)境搭建的詳細教程
這篇文章主要介紹了MyBatis-Plus集成Druid環(huán)境搭建的詳細教程,需要的朋友可以參考下2020-08-08
詳解如何使用MongoDB+Springboot實現(xiàn)分布式ID的方法
這篇文章主要介紹了詳解如何使用MongoDB+Springboot實現(xiàn)分布式ID的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09

