response 流 文件下载

1,由于参数数据量大使用form,post的方式调后台,传参数,数据量大一定要用post方式

<form id="downloadForm" method="post" action="<%=request.getContextPath()%>/business/com.asiainfo.boss.so.order.base.web.soframe.FileInputUtilAction?action=createDownExcel">
  <input type="hidden" id="ERRORMSG" name="ERRORMSG"/>
 </form>

2、在jsp中用js调用:errMsg是要传到后台解析生成文件的数据

document.getElementById("ERRORMSG").value = errMsg;
document.getElementById("downloadForm").submit();

3、后台代码:

 public void createDownExcel(HttpServletRequest request,HttpServletResponse response)throws Exception{
     try {
      
      String errMsg=HttpUtil.getAsString(request, "ERRORMSG");
      HSSFWorkbook workbook =null;
   if(errMsg!=null&&!"".equals(errMsg)){
    String[] errMsgArr=errMsg.split("\\$\\$");
    if(errMsgArr!=null&&errMsgArr.length>0){
      workbook= new HSSFWorkbook();
      HSSFSheet sheet =workbook.createSheet("failrecord");
      for(int i=0;i<errMsgArr.length;i++){
       HSSFRow row=sheet.createRow(i);
      String tempMsg[]=errMsgArr[i].split("#");
      String tipMsg=tempMsg[1];
      String[] colMsgArr=tempMsg[0].split(",");
      for(short j=0;j<colMsgArr.length;j++){
       HSSFCell cell=row.createCell(j);

       cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置编码
       cell.setCellValue(colMsgArr[j]);
       
      }
       HSSFCell celltip=row.createCell((short)colMsgArr.length);
      celltip.setEncoding(HSSFCell.ENCODING_UTF_16);//设置编码
      celltip.setCellValue(tipMsg);      

      System.out.println(tipMsg);
     }
      String fileName = "fail_records.xls";
      response.reset();
      response.setCharacterEncoding("UTF-8");
      response.setContentType("application/x-download;charset=UTF-8");
      response.setHeader("Content-Disposition", "attachment;charset=UTF-8;filename=\""+ toUtf8String(fileName) + "\"");

      workbook.write(response.getOutputStream());
      response.getOutputStream().flush();
      response.getOutputStream().close();
    }
   }
  } catch (Exception e) {
   log.error("导出错误数据出错,错误信息:"+e.getMessage());
  }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值