亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用a標簽進行下載excel文件,打開下載后的文件內容為空,這是為什么?

使用a標簽進行下載excel文件,打開下載后的文件內容為空,這是為什么?

慕沐1134704 2019-04-19 11:24:36
前端代碼:<a?:href="'/api/down-api/down'"?download="down.xlsx">下載</a>后端代碼:接口:@ApiOperation(value?=?"下載") @GetMapping("/down") public?void?downTemplate(HttpServletResponse?response)?{ ????String?fileName?=?"down.xlsx"; ????couponService.down(response,?fileName); }實現類:public?void?down(HttpServletResponse?response?,String?filename)?{ ????//?讀取要下載的文件,保存到文件輸入流 ????FileInputStream?in?=?null; ????OutputStream?out?=?null; ????try?{ ????????//?得到要下載的文件(文件存放在項目resources的static文件夾里) ????????File?file?=?new?File("src\\main\\resources\\static"?+?"\\"?+?filename); ????????//?如果文件不存在 ????????if?(!file.exists())?{ ????????????throw?new?BusinessException("Could?not?read?file"); ????????} ????????//?設置響應頭,控制瀏覽器下載該文件 ????????response.setHeader("Content-Disposition",?"attachment;filename="+filename); ????????response.setHeader("Content-Type",?"application/octet-stream;charset=UTF-8"); ????????response.setContentType("application/octet-stream"); ????????in?=?new?FileInputStream(file); ????????//?創建輸出流 ????????out?=?response.getOutputStream(); ????????//?創建緩沖區 ????????byte?buffer[]?=?new?byte[1024]; ????????int?len?=?0; ????????//?循環將輸入流中的內容讀取到緩沖區當中 ????????while?((len?=?in.read(buffer))?>?0)?{ ????????????//?輸出緩沖區的內容到瀏覽器,實現文件下載 ????????????out.write(buffer,?0,?len); ????????} ????????//?關閉文件輸入流 ????????in.close(); ????????//?關閉輸出流 ????????out.close(); ????}?catch?(Exception?e)?{ ????????e.printStackTrace(); ????} }
查看完整描述

1 回答

?
cxxyjsj

TA貢獻119條經驗 獲得超22個贊

后臺報錯了嗎

查看完整回答
反對 回復 2019-04-19
  • 1 回答
  • 0 關注
  • 7116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號