为了账号安全,请及时绑定邮箱和手机立即绑定

使用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 关注
  • 6758 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信