Error : IllegalStateException in Java. Cannot forward after response has been committed
Error : IllegalStateException in Java. Cannot forward after response has been committed
需要帮助,
我正在努力在 Java Servlet 中下载文件,文件下载后无法将请求发送到页面。
文件下载成功,当我尝试将请求转发到页面时出现 illegalStateException。
这是代码
public void fileDownload(String stringFileToDownload, HttpServletResponse response) throws Exception{
FileInputStream inStream = null;
OutputStream outStream = null;
try{
File downloadFile = new File(stringFileToDownload); //Reads input file
inStream = new FileInputStream(downloadFile);
response.setContentType("application/zip-compressed"); //MIME type of the file
response.setContentLength((int) downloadFile.length());
response.setHeader("Content-Disposition", "attachment; filename=Time.zip");
//response's output stream
outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
catch(Exception ex){
throw ex;
}
finally{
//response.flushBuffer();
try{
if(inStream != null){
inStream.close();
}
if(outStream != null){
//outStream.flush();
outStream.close();
}
}
catch(Exception ex){
throw ex;
}
}
}
我从servlet调用了这个方法;从 servlet 重定向到另一个页面
调用代码:
FileDownloadFromWeb fileDownloadFromWeb = new FileDownloadFromWeb();
fileDownloadFromWeb.fileDownload(stringarchiveFile, response); //Allow to download
Request Dispatcher objRequestDispatcher = request.getRequestDispatcher(objProperties.getProperty("SUCCESS_DOWNLOAD"));
objRequestDispatcher.forward(request, response);
此处,当您写入输出流时,响应将被提交 (outStream.write(buffer, 0, bytesRead);
),然后如果您尝试使用 request.forward(),它将反抗地抛出错误。
此问题的解决方案是将您的文件内容设置为请求参数中的对象,而不是在 jsp 中使用它或使用重定向而不是转发
可能您的问题可以通过使用单独的 servlet 进行文件下载来解决,请参阅此 after download a image ,i want to redirect on another page, but not able to
需要帮助,
我正在努力在 Java Servlet 中下载文件,文件下载后无法将请求发送到页面。
文件下载成功,当我尝试将请求转发到页面时出现 illegalStateException。
这是代码
public void fileDownload(String stringFileToDownload, HttpServletResponse response) throws Exception{
FileInputStream inStream = null;
OutputStream outStream = null;
try{
File downloadFile = new File(stringFileToDownload); //Reads input file
inStream = new FileInputStream(downloadFile);
response.setContentType("application/zip-compressed"); //MIME type of the file
response.setContentLength((int) downloadFile.length());
response.setHeader("Content-Disposition", "attachment; filename=Time.zip");
//response's output stream
outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
catch(Exception ex){
throw ex;
}
finally{
//response.flushBuffer();
try{
if(inStream != null){
inStream.close();
}
if(outStream != null){
//outStream.flush();
outStream.close();
}
}
catch(Exception ex){
throw ex;
}
}
}
我从servlet调用了这个方法;从 servlet 重定向到另一个页面
调用代码:
FileDownloadFromWeb fileDownloadFromWeb = new FileDownloadFromWeb();
fileDownloadFromWeb.fileDownload(stringarchiveFile, response); //Allow to download
Request Dispatcher objRequestDispatcher = request.getRequestDispatcher(objProperties.getProperty("SUCCESS_DOWNLOAD"));
objRequestDispatcher.forward(request, response);
此处,当您写入输出流时,响应将被提交 (outStream.write(buffer, 0, bytesRead);
),然后如果您尝试使用 request.forward(),它将反抗地抛出错误。
此问题的解决方案是将您的文件内容设置为请求参数中的对象,而不是在 jsp 中使用它或使用重定向而不是转发
可能您的问题可以通过使用单独的 servlet 进行文件下载来解决,请参阅此 after download a image ,i want to redirect on another page, but not able to