• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

java.lang.IllegalStateException: getOutputStream() error

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Dibyendu Sarkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if you use an Ajax technique to refresh the page, what's the matter with returning a null ModelAndView ?
 
Beware the other head of science - it bites! Nibble on this message:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic