`

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());
  }
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics