Java 正在尝试使用ApachePOI下载Struts1.3中的Excel。从第二个请求开始不工作
Struts_config.xmlJava 正在尝试使用ApachePOI下载Struts1.3中的Excel。从第二个请求开始不工作,java,excel,apache-poi,struts1,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>
<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();
}
String serverFilePath=“D:\\workbook8.xls”;
//通过会话属性获取参数。
HttpSession HttpSession=request.getSession();
String filterStr=(String)httpSession.getAttribute(“filterStr”);
LinkedHashMap bzMap=(LinkedHashMap)httpSession.getAttribute(“BizMap”);
//写入服务器中的文件系统
writetoFile(serverFilePath、filterStr、bzMap);
//从服务器读取文件并流式传输
文件下载文件=新文件(serverFilePath);
字符串mimeType=getServlet().getServletContext().getMimeType(serverFilePath);
response.setContentType(mimeType);
response.setContentLength((int)downloadFile.length());
response.setHeader(“内容处置”、“附件;文件名=“+serverFilePath”);
ServletOutputStream out=response.getOutputStream();
FileInputStream inStream=新的FileInputStream(下载文件);
字节[]输出缓冲区=新字节[4096];
while(流内读取(outputBuffer,04096)!=-1){
写(outputBuffer,04096);
}
流内关闭();
out.flush();
out.close();
public void writetoFile(字符串文件路径、字符串过滤器tr、,
LinkedHashMap bizzMap){
HSSFWorkbook wb=新的HSSFWorkbook();
HSSFSheet sheet=wb.createSheet(“业务详细信息”);
HSSFRow行=sheet.createRow(0);
Cell Cell=row.createCell(columnNum);
cell.setCellValue(celvalue);
//写内容的逻辑
:
:
FileOutputStream fileOut=新的FileOutputStream(filePath);
wb.写入(文件输出);
fileOut.close();
wb.close();
}
如果我对read和stream部分进行注释,则会根据服务器中针对所有请求的筛选器参数正确创建excel,但与read部分一起,excel仅在第一次请求时创建,并在客户端中再次返回相同的部分-调试表明它不属于此特定操作类本身。它也不会抛出任何异常