尝试使用Apache Poi在Struts1.3中下载Excel。不在第二个请求中工作

时间:2015-09-01 05:40:56

标签: java excel apache-poi struts1

Struts_config.xml

<action path="/dwnldExcel" type="ntrs.pat.action.DwnExclAction">
</action>

rightMenuPanel.jsp

<html:link action="dwnldExcel"><img src="resource/excel_icon.png" height="30" width="30"></html:link>

xxAction.java

   String serverFilePath = "D:\\workbook8.xls";
  //Getting the parameters via session attribute.     
   HttpSession httpSession = request.getSession();
   String filterStr = (String) httpSession.getAttribute("filterStr");
   LinkedHashMap<String, ArrayList<String>> bzMap = (LinkedHashMap<String,   ArrayList<String>>) httpSession.getAttribute("BizMap");

  //Write to filesytem in server
   writetoFile(serverFilePath, filterStr, bzMap);

  // Read the file from server and stream it
   File downloadFile = new File(serverFilePath);
   String mimeType = getServlet().getServletContext().getMimeType(serverFilePath);
   response.setContentType(mimeType);
   response.setContentLength((int) downloadFile.length());
   response.setHeader("Content-Disposition","attachment;filename="+serverFilePath );
   ServletOutputStream out = response.getOutputStream();
   FileInputStream inStream = new FileInputStream(downloadFile);     
   byte[] outputBuffer  = new byte[4096];
   while(inStream.read(outputBuffer, 0, 4096) != -1){
        out.write(outputBuffer, 0, 4096);
   }
   inStream.close();
   out.flush();
   out.close();

public void writetoFile(String filePath, String filterStr, 
            LinkedHashMap<String, ArrayList<String>> bizzMap){
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("BusinessDetails");

    HSSFRow row = sheet.createRow(0);
    Cell cell = row.createCell(columnNum);
    cell.setCellValue(celvalue);
    //logic to write the content
    :
    :
    FileOutputStream fileOut = new FileOutputStream(filePath);
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}

如果我对read和stream部分发表评论,则会根据服务器中针对所有请求的过滤器参数正确创建excel,但是与读取部分一起 - 只在第一次请求时创建excel,并再次返回相同的excel。客户端 - 调试显示它不会来到这个特定的动作类本身。它也没有抛出任何异常。

0 个答案:

没有答案