Springboot導(dǎo)出文件,前端下載文件

后端代碼:

可以把請求設(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í)才不會報(bào)“Excel 已完成文件級驗(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請求

? ? ? ? ? ? ? ? method: 'post',

? ? ? ? ? ? ? ? url: 'http://127.0.0.1:8762/dataService/download', // 請求地址

? ? ? ? ? ? ? ? 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);

? ? ? ? ? ? });

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容