chrome 下载excel时,失败,网络错误的问题

需要在response中添加  response.addHeader("content-length",length);
length代表文件长度 获取wb长度的方法
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos); 
int length = baos.size();
response.addHeader("content-length",length+"");

完整方法如下
public static void downloadExcel(HttpServletResponse response,Workbook wb,String fileName) throws Exception{
   try {
      response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("utf-8"),"iso-8859-1"));
      response.setContentType("application/ynd.ms-excel;charset=UTF-8");
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      wb.write(baos);
      int length = baos.size();
           response.addHeader("content-length",length+"");
      OutputStream out=response.getOutputStream();
      wb.write(out);
      out.flush();
      out.close();
   } catch (IOException e) {
      e.printStackTrace();
   }
}
    原文作者:惊云鸟
    原文地址: https://blog.csdn.net/zyc050707/article/details/107047494
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞