I recently implemented a functionality for uploading and downloading files from a file server (SFTP). My app is based on spring framework. The overall functionality is working fine and I can upload and download files from the server. However I'm getting some error messages in my log files. Before I go on to the errors, let me explain just a little bit about how the upload works:
When the user uploads the file, the overall form is not submitted. Rather than submitting the form, I'm calling my own methods to upload the file to the server.
Once the file's uploaded, the file shows up on the current form with a download icon. Now, the user can continue to upload files and go ahead submit the form. OR, they can decide to click on the download icon and download the already uploaded file. (For whatever reason). Now here's where the problem lies. Since the download methods make use of the response object, once the user clicks on the download icon, my response object is used up and the headers/disposition etc. are set.
Now say the user is trying to download the second file, i see the following errors in my log:
"Servlet.service() for servlet cfg2app threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response"
As far as I understand, essentially, per-request we only get one response object. After that, we need some way to make the client initiate a new response. And the getOutputStream() once called, can not be called again etc.
I know this is possible because any modern webmails (gmail e.g.) they let us upload files, and then download them to see them before we send the email.
So can anyone shed some light as to what can be done to handle this situation. It is not breaking my application, but this is an error and needs to be handled.
Not quite sure how you're doing this or if you're using Spring MVC.
If you are, you should return null in your Controller implementation when it expects you to to return a ModelAndView. This tells the DispatcherServlet workflow that you've handled the response yourself and there is no View to render.
I'm also not sure if you know it, but Spring MVC provides support for Jakarta Commons FileUpload to support this kind of use case. It is an alternative to reinventing the wheel, unless of course you want to reinvent the wheel.
kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Yes am aware of AJAX and have used it in a few places. It just didn't occur to me that of course it can be used in places other than just calling web services, displaying responses from web services etc. without refreshing the page. Yes, of course they used AJAX! What was I even thinking! It's google after all... who were responsible for taking an existing wine and packaging it in a new much much shiny bottle!
Yes Ken, I'm using Spring MVC. I came to know about Jakarta Commons Fileupload when I was implementing this functionality. But I'm not sure if Jakarta commons provided for SFTP uploads? At least I couldn't confirm that. So went with other APIs. As for uploading my file to the app server, I used Springs support for the multipart requests.
Anyway, don't think I can try with the null. I'm doing the upload in the overriden method of processFormSubmission where I check whether user just cliked on Update/Save/Upload/Download/Submit... based on this action, I do my processing. But in the end of each "if" (for checking the action) i call showForm(request, response, error)... like below...
you can see my obvious problem with returning null...