struts下载文件时出错:
java.lang.IllegalStateException:
Cannot forward after response has been committed原代码:
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String strFileName = request.getParameter( "fujian ");
System.out.println(strFileName);
String filepath=request.getRealPath( "/ ");
File file = new File(filepath+ "/fujian/ " + strFileName);//
if(file.exists()){
try{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
strFileName = java.net.URLEncoder.encode(strFileName, "UTF-8 ");//处理中文文件名的问题
strFileName = new String(strFileName.getBytes( "UTF-8 "), "GBK ");//处理中文文件名的问题
response.reset();
response.setCharacterEncoding( "UTF-8 ");
response.setContentType( "application/x-rar-compressed ");//不同类型的文件对应不同的MIME类型
response.setHeader( "Content-Disposition ", "attachment; filename= "+strFileName);
OutputStream os = response.getOutputStream();
while(bis.read(buffer) > 0){
os.write(buffer);
}
bis.close();
os.close();
return mapping.findForward( "suc ");
}
catch(Exception e){
return mapping.findForward( "fail ");
}
}else{
return mapping.findForward( "fail ");
}
请问什么原因?
------解决方案--------------------当文件下载框弹出来之后,应该不能再转向了,你 return null 吧,不知道大家有没好的办法
------解决方案--------------------在response完成之后,不能forward。你就要检查一下forward之前代码
------解决方案--------------------Cannot forward after response
看看你的struts_config.xml的配置有没有fail的