aspose file tools*
The moose likes Servlets and the fly likes java.lang.IllegalStateException: getOutputStream() error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.lang.IllegalStateException: getOutputStream() error" Watch "java.lang.IllegalStateException: getOutputStream() error" New topic
Author

java.lang.IllegalStateException: getOutputStream() error

Dibyendu Sarkar
Greenhorn

Joined: Jan 12, 2009
Posts: 25
Hi,

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.

Any help is highly appreciated.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Dibyendu Sarkar wrote:
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.
They use ajaxical techniques. Asynchronous Javascript And Xml. Pretty self-descriptive ;)

If you're new to the concept, you may find this tutorial useful: http://www.w3schools.com/Ajax/Default.Asp
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
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
Dibyendu Sarkar
Greenhorn

Joined: Jan 12, 2009
Posts: 25
Hi Bauke,

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...

Any suggestions?
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
So if you use an Ajax technique to refresh the page, what's the matter with returning a null ModelAndView ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.IllegalStateException: getOutputStream() error