Hello and thank you for reading my post.

My question is about downloading a file using a servlet and handling
possible errors that may take place during the download process.

1) I have a JSP page "download-file.jsp" with a "Download file" button:
======================================================
<form  method="post" action="do_download_file">
  [...]
  <input type="submit" name="download_file" id="download_file"
value="Download file" />
  [...]
</form>
======================================================
When this button is hit by a user, the "doPost()" method of the servlet
mapped with the action "do_download_file" is executed.
It includes a call to the method below.

2) Everywhere on the Web one can find that type of code (for the file
downloading):
======================================================
public boolean downloadFile(String s_fileNameAndPath, String s_fileName,
HttpServletResponse httpServletResponse)
throws IOException, SQLException, ClassNotFoundException, ModelException
{
File file = null;
long l_fileLength = -1;
ServletOutputStream servletOutputStream = null;
int n_bufsize = 4096;
DataInputStream dataInputStream = null;
int n_nbBytesRead = -1;
boolean b_anErrorOccurred = false;
byte[] byteBuffer;
                
                file = new File(s_fileNameAndPath);
                
                l_fileLength = file.length();
                
                if(l_fileLength <= 0)
                {
                                b_anErrorOccurred = true;
                                return b_anErrorOccurred;
                }
                
                n_nbBytesRead = 0;
                byteBuffer = new byte[n_bufsize];
                
                servletOutputStream = httpServletResponse.getOutputStream();
                
                httpServletResponse.setContentType("text/html");
                httpServletResponse.setContentLength((int) l_fileLength);
                
                httpServletResponse.setHeader("Content-Disposition", 
"attachment;
filename=\"" + s_fileName + "\"");
                
                dataInputStream = new DataInputStream(new 
FileInputStream(file));
                
                while((dataInputStream != null)
                         && ((n_nbBytesRead = dataInputStream.read(byteBuffer)) 
!= -1))
                {
                                servletOutputStream.write(byteBuffer, 0, 
n_nbBytesRead);
                                servletOutputStream.flush();
                }
                
                if(true)
                {
                                throw new ModelException("To provoke an error 
on purpose.");
                }
                
                dataInputStream.close();
                servletOutputStream.close();
                
                return b_anErrorOccurred;
}
======================================================
(Apart from the exception being thrown) this code works: it actually
downloads the targeted file.

You may have noticed that in the method above, I deliberately throw an
exception so that I can test error handling.

My problem is that, with this way of doing things, I can't tell the user
that the download operation failed since the "HttpServletResponse" has
already been "consumed" when the method returns...

My question is:
Ideally, after the download, I wish I could go back to the JSP
"download-file.jsp" so that I can tell the user that an error occurred while
downloading the file.
If the download was successful, I also would like to tell the user that
everything went right...
I don't see how I can both download the file and redirect the user towards
the "download-file.jsp" page with an appropriate message.
Do you have an idea how I should proceed?

All the best and sorry for the language approximations like "consumed", etc.



--
View this message in context: 
http://tomcat.10.x6.nabble.com/File-download-using-a-servlet-and-error-handling-tp5023017.html
Sent from the Tomcat - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to