SpringBoot下載文件遇到文件損壞等問題解決方案
問題一:下載的文件名稱出現(xiàn)中文亂碼的問題
解決方案:
response.setHeader("Content-Disposition",
"attachment;filename=" + new String("下載模板".getBytes("UTF-8"), "ISO8859-1"));問題二:在swagger中測試下載接口,點擊下載的文件,發(fā)現(xiàn)文件名是亂碼的問題
解決方案:
response.setHeader("Content-Disposition", "attachment;fileName=" +
URLEncoder.encode("線索導(dǎo)入模板.xlsx","utf8"));說明:通過URLEncoder.encode函數(shù)對文件名稱處理后,無論是在瀏覽器調(diào)用GET請求下載文件,還是Swagger中調(diào)用下載接口,都不會出現(xiàn)文件名亂碼問題。
問題三:下載的excel文件打開時總是提示部分內(nèi)容有問題,嘗試恢復(fù)。

問題原因:
一般有2種情況:
1、由于沒有找到文件,下載的文件字節(jié)大小為0,這種情況文件完全打不開
2、讀取的文件大小和元素文件的大小不一致,這種情況會提升自動修復(fù)。
解決辦法:
網(wǎng)上最多的解決方案是主動在response的Header中設(shè)置Content-Length大小。但這種方式其實是錯誤的。文件的Content-Length其實可以從返回流中直接獲取,并不需要用戶主動去設(shè)置。這里的問題核心應(yīng)該是思考:為什么下載的文件和元素文件的大小會不一致?
下面的2個獲取inputStream的長度的API,只有在讀取磁盤上具體文件中才比較適用。如果是jar包中的文件,是獲取不到大小。
加上設(shè)置大小:
response.addHeader("Content-Length",String.valueOf(file.length()));
//response.addHeader("Content-Length",String.valueOf(inputStream.available()));問題四:采用BufferedInputStream緩沖流讀寫文件導(dǎo)致輸出文件和原始文件體積差異的問題
由于下載的文件體積總是比元素文件體積大一點點,導(dǎo)致文件打開提示異常修復(fù)。
outputStream = response.getOutputStream();
bis = new BufferedInputStream(inputStream);
//緩沖數(shù)組,每次讀取1024
byte[] buff = new byte[1024];
while (bis.read(buff)!= -1) {
//異常代碼行
outputStream.write(buff, 0, buff.length);
}
outputStream.flush(); 原因分析:
出現(xiàn)問題的原因就是buff.length,數(shù)組聲明后長度就是固定的,而不是獲取里面讀取的內(nèi)容的字節(jié)長度,所以導(dǎo)致這里的buff.length的值始終是1024。
解決方案:
outputStream = response.getOutputStream();
bis = new BufferedInputStream(inputStream);
//緩沖流,每次讀取1024
byte[] buff = new byte[1024];
int readLength = 0;
while (( readLength = bis.read(buff)) != -1) {
//每次寫入緩沖流buff讀到的字節(jié)長度,而不是buff.length
outputStream.write(buff, 0, readLength);
}
outputStream.flush();問題五:開發(fā)環(huán)境下載成功,打成jar包發(fā)布到服務(wù)器上部署就出現(xiàn)下載失敗問題
原因:
Resource下的文件是存在于jar這個文件里面,在磁盤上是沒有真實路徑存在的,它其實是位于jar內(nèi)部的一個路徑。所以通過ResourceUtils.getFile或者this.getClass().getResource("")方法無法正確獲取文件。
解決:
通過ClassPathResource讀取文件流
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("template/template.xlsx");下載方式
方式一:經(jīng)典的緩沖流BufferedInputStream讀取法,一般讀取比較大的文件,優(yōu)先考慮緩沖流讀取方式
方式二:利用spring的FileCopyUtils工具類,小文件優(yōu)先考慮此方式,代碼更簡單不易出錯。
實例
1、控制層代碼
@Operation(summary = "下載模版",description = "下載模版")
@GetMapping("/download")
public void download(HttpServletResponse response){
templateService.download(response);
}/**
* 下載線索模板
* @param response
*/
public void download(HttpServletResponse response) {
InputStream inputStream = null;
BufferedInputStream bis = null;
OutputStream outputStream = null;
try {
inputStream=getClass().getClassLoader().getResourceAsStream("template/template.xlsx");
response.setContentType("application/octet-stream");
response.setHeader("content-type", "application/octet-stream");
//待下載文件名
String fileName = URLEncoder.encode("模板.xlsx","utf8");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
outputStream = response.getOutputStream();
//加上設(shè)置大小 下載下來的excel文件才不會在打開前提示修復(fù)
//這里流的長度很難在開始讀取前獲取,特別是打成jar包后,讀取inputStream長度經(jīng)常失敗
//response.addHeader("Content-Length",String.valueOf(classPathResource.getFile().length()));
//response.addHeader("Content-Length",String.valueOf(inputStream.available()));
//方式一:經(jīng)典的緩沖流BufferedInputStream讀取法
// bis = new BufferedInputStream(inputStream);
//緩沖流,每次讀取1024
// byte[] buff = new byte[1024];
// int readLength = 0;
// while (( readLength = bis.read(buff)) != -1) {
// outputStream.write(buff, 0, readLength);
// }
// outputStream.flush();
//方式二:利用spring的FileCopyUtils工具類
if(inputStream!=null){
byte[] results = FileCopyUtils.copyToByteArray(inputStream);
outputStream.write(results);
outputStream.flush();
}
} catch ( IOException e ) {
log.error("文件下載失敗,e");
} finally {
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(bis);
}
}以上就是SpringBoot下載文件遇到文件損壞等問題解決方案的詳細內(nèi)容,更多關(guān)于SpringBoot下載文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Eclipse新建項目不可選擇Java Project問題解決方案
這篇文章主要介紹了Eclipse新建項目不可選擇Java Project問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07
springboot使用jasypt對配置文件加密加密數(shù)據(jù)庫連接的操作代碼
這篇文章主要介紹了springboot使用jasypt對配置文件加密加密數(shù)據(jù)庫連接的操作代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01

