Springboot導(dǎo)出文件,前端下載文件方式
Springboot導(dǎo)出文件,前端下載文件
后端代碼
可以把請(qǐng)求設(shè)置為post,我這里是Get
@RequestMapping(value = "/download", method = RequestMethod.POST)
public void download(HttpServletRequest request, HttpServletResponse res) throws Exception {
File excelFile = new File("/Users/i501695/GitHUbProject/EN_ProductIntergration/databaseclient/src/main/resources/Files/ProductTemplateCopy.xlsx");
res.setCharacterEncoding("UTF-8");
String realFileName = excelFile.getName();
res.setHeader("content-type", "application/octet-stream;charset=UTF-8");
res.setContentType("application/octet-stream;charset=UTF-8");
//加上設(shè)置大小下載下來的.xlsx文件打開時(shí)才不會(huì)報(bào)“Excel 已完成文件級(jí)驗(yàn)證和修復(fù)。此工作簿的某些部分可能已被修復(fù)或丟棄”
res.addHeader("Content-Length", String.valueOf(excelFile.length()));
try {
res.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(realFileName.trim(), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = res.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(excelFile));
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
前端偽代碼結(jié)合Axios(核心代碼一樣,只是結(jié)合了Axios)
Axios({ // 用axios發(fā)送post請(qǐng)求
method: 'post',
url: 'http://127.0.0.1:8762/dataService/download', // 請(qǐng)求地址
data: formData, // 參數(shù)
responseType: 'blob' // 表明返回服務(wù)器返回的數(shù)據(jù)類型
})
.then((res) => { // 處理返回的文件流
let blob = new Blob([res.data], {type: res.data.type})
const fileName = 'ProductTemplateCopy.xlsx';
let downloadElement = document.createElement('a')
let href = window.URL.createObjectURL(blob); //創(chuàng)建下載的鏈接
downloadElement.href = href;
downloadElement.download = fileName; //下載后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); //點(diǎn)擊下載
document.body.removeChild(downloadElement); //下載完成移除元素
window.URL.revokeObjectURL(href); //釋放blob
message.success('upload successfully.');
})
.catch(function (error) {
console.log(error);
});
SpringBoot文件下載的幾種方式
1. 將文件以流的形式一次性讀取到內(nèi)存
通過響應(yīng)輸出流輸出到前端
/**
* @param path 想要下載的文件的路徑
* @param response
* @功能描述 下載文件:
*/
@RequestMapping("/download")
public void download(String path, HttpServletResponse response) {
try {
// path是指想要下載的文件的路徑
File file = new File(path);
log.info(file.getPath());
// 獲取文件名
String filename = file.getName();
// 獲取文件后綴名
String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
log.info("文件后綴名:" + ext);
// 將文件寫入輸入流
FileInputStream fileInputStream = new FileInputStream(file);
InputStream fis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 設(shè)置response的Header
response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用:告知瀏覽器以何種方式顯示響應(yīng)返回的文件,用瀏覽器打開還是以附件的形式下載到本地保存
//attachment表示以附件方式下載 inline表示在線打開 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默認(rèn)名稱,因?yàn)榫W(wǎng)絡(luò)傳輸只支持URL編碼的相關(guān)支付,因此需要將文件名URL編碼后進(jìn)行傳輸,前端收到后需要反編碼才能獲取到真正的名稱
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 告知瀏覽器文件的大小
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
2. 將輸入流中的數(shù)據(jù)循環(huán)寫入到響應(yīng)輸出流中
而不是一次性讀取到內(nèi)存,通過響應(yīng)輸出流輸出到前端
/**
* @param path 指想要下載的文件的路徑
* @param response
* @功能描述 下載文件:將輸入流中的數(shù)據(jù)循環(huán)寫入到響應(yīng)輸出流中,而不是一次性讀取到內(nèi)存
*/
@RequestMapping("/downloadLocal")
public void downloadLocal(String path, HttpServletResponse response) throws IOException {
// 讀到流中
InputStream inputStream = new FileInputStream(path);// 文件的存放路徑
response.reset();
response.setContentType("application/octet-stream");
String filename = new File(path).getName();
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
ServletOutputStream outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int len;
//從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲(chǔ)在緩沖區(qū)字節(jié)數(shù)組中,讀到末尾返回-1
while ((len = inputStream.read(b)) > 0) {
outputStream.write(b, 0, len);
}
inputStream.close();
}
3. 下載網(wǎng)絡(luò)文件到本地
/**
* @param path 下載后的文件路徑和名稱
* @param netAddress 文件所在網(wǎng)絡(luò)地址
* @功能描述 網(wǎng)絡(luò)文件下載到服務(wù)器本地
*/
@RequestMapping("/netDownloadLocal")
public void downloadNet(String netAddress, String path) throws IOException {
URL url = new URL(netAddress);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(path);
int bytesum = 0;
int byteread;
byte[] buffer = new byte[1024];
while ((byteread = inputStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fileOutputStream.write(buffer, 0, byteread);
}
fileOutputStream.close();
}
4. 網(wǎng)絡(luò)文件獲取到服務(wù)器后
經(jīng)服務(wù)器處理后響應(yīng)給前端
/**
* @param netAddress
* @param filename
* @param isOnLine
* @param response
* @功能描述 網(wǎng)絡(luò)文件獲取到服務(wù)器后,經(jīng)服務(wù)器處理后響應(yīng)給前端
*/
@RequestMapping("/netDownLoadNet")
public void netDownLoadNet(String netAddress, String filename, boolean isOnLine, HttpServletResponse response) throws Exception {
URL url = new URL(netAddress);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
response.reset();
response.setContentType(conn.getContentType());
if (isOnLine) {
// 在線打開方式 文件名應(yīng)該編碼成UTF-8
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(filename, "UTF-8"));
} else {
//純下載方式 文件名應(yīng)該編碼成UTF-8
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
}
byte[] buffer = new byte[1024];
int len;
OutputStream outputStream = response.getOutputStream();
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
}
5. 常見異常和問題
(1)響應(yīng)對(duì)象無需通過return返回

原因: 響應(yīng)對(duì)象是可以不用作為方法返回值返回的,其在方法執(zhí)行時(shí)已經(jīng)開始輸出,且其無法與@RestController配合,以JSON格式返回給前端


解決辦法: 刪除return語句
(2)返回前端的文件名必須進(jìn)行URL編碼
原因: 網(wǎng)絡(luò)傳輸只能傳輸特定的幾十個(gè)字符,需要將漢字、特殊字符等經(jīng)過Base64等編碼來轉(zhuǎn)化為特定字符,從而進(jìn)行傳輸,而不會(huì)亂碼
URLEncoder.encode(fileName, "UTF-8")
(3)IO流有待學(xué)習(xí)
1:read() :
從輸入流中讀取數(shù)據(jù)的下一個(gè)字節(jié),返回0到255范圍內(nèi)的int字節(jié)值。如果因?yàn)橐呀?jīng)到達(dá)流末尾而沒有可用的字節(jié),則返回-1。在輸入數(shù)據(jù)可用、檢測到流末尾或者拋出異常前,此方法一直阻塞。
2:read(byte[] b) :
從輸入流中讀取一定數(shù)量的字節(jié),并將其存儲(chǔ)在緩沖區(qū)數(shù)組 b 中。以整數(shù)形式返回實(shí)際讀取的字節(jié)數(shù)。在輸入數(shù)據(jù)可用、檢測到文件末尾或者拋出異常前,此方法一直阻塞。如果 b 的長度為 0,則不讀取任何字節(jié)并返回 0;否則,嘗試讀取至少一個(gè)字節(jié)。如果因?yàn)榱魑挥谖募┪捕鴽]有可用的字節(jié),則返回值 -1
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java將指定目錄下文件復(fù)制到目標(biāo)文件夾的幾種小方法
在Java中有多種方法可以實(shí)現(xiàn)文件的復(fù)制,這篇文章主要給大家介紹了關(guān)于java將指定目錄下文件復(fù)制到目標(biāo)文件夾的幾種小方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
使用SpringBoot動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)方式
在我們企業(yè)項(xiàng)目開發(fā)的過程中,有的時(shí)候,一個(gè)項(xiàng)目需要在運(yùn)行時(shí),根據(jù)某種條件選擇使用哪個(gè)數(shù)據(jù)源,那么此時(shí)該怎么進(jìn)行動(dòng)態(tài)切換呢,本文給大家例舉一種常見的實(shí)現(xiàn)方式,文中有詳細(xì)的實(shí)現(xiàn)步驟,需要的朋友可以參考下2023-12-12
JAVA 文件監(jiān)控 WatchService的示例方法
本篇文章主要介紹了JAVA 文件監(jiān)控 WatchService的示例方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
nacos注冊(cè)中心單節(jié)點(diǎn)ap架構(gòu)源碼解析(最新推薦)
這篇文章主要介紹了nacos注冊(cè)中心單節(jié)點(diǎn)ap架構(gòu)源碼解析,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
SpringBoot預(yù)防XSS攻擊的實(shí)現(xiàn)
XSS攻擊是一種在web應(yīng)用中的計(jì)算機(jī)安全漏洞,它允許惡意web用戶將代碼植入到提供給其它用戶使用的頁面,本文主要介紹了SpringBoot預(yù)防XSS攻擊的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08
Java AOP實(shí)現(xiàn)自定義滑動(dòng)窗口限流器方法詳解
這篇文章主要介紹了Java AOP實(shí)現(xiàn)自定義滑動(dòng)窗口限流器方法,其中滑動(dòng)窗口算法彌補(bǔ)了計(jì)數(shù)器算法的不足,滑動(dòng)窗口算法把間隔時(shí)間劃分成更小的粒度,當(dāng)更小粒度的時(shí)間間隔過去后,把過去的間隔請(qǐng)求數(shù)減掉,再補(bǔ)充一個(gè)空的時(shí)間間隔,需要的朋友可以參考下2022-07-07
詳解Spring與Mybatis整合方法(基于IDEA中的Maven整合)
這篇文章主要介紹了Spring與Mybatis整合方法(基于IDEA中的Maven整合),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Spring?Security實(shí)現(xiàn)接口放通的方法詳解
在用Spring?Security項(xiàng)目開發(fā)中,有時(shí)候需要放通某一個(gè)接口時(shí),我們需要在配置中把接口地址配置上,這樣做有時(shí)候顯得麻煩。本文將通過一個(gè)注解的方式快速實(shí)現(xiàn)接口放通,感興趣的可以了解一下2022-05-05
Executor攔截器高級(jí)教程QueryInterceptor的規(guī)范
今天小編就為大家分享一篇關(guān)于Executor攔截器高級(jí)教程QueryInterceptor的規(guī)范,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

