wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet writes file that is sometimes corrupt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet writes file that is sometimes corrupt" Watch "servlet writes file that is sometimes corrupt" New topic
Author

servlet writes file that is sometimes corrupt

Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38
Howdy Ranchers. I have a simple servlet that produces a jpg based on posted data. If I simply return the image it always works. However, if I create the file on the server filesystem, and forward to a view that links to it, sometimes the image file is corrupt. If I reload a few times, I'll get a good version, and reloading again might produce a corrupt one. I'm wondering if one of you might notice something obvious in my code that might explain the inconsistent result, or have any other idea about why this is happening.

Thanks!

Mike


The result jsp:



SCJP 5, SCWCD 5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Where is the image data coming from? If it's a file upload from the client, then you are most definitely doing it wrong.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38
Hi Bear. Thanks much for the reply. The image data comes from a flash module, the source of which I don't have :-(. I did just no notice I was processing the output stream differently. In the case that always worked:



And in the case that wasn't:



When I added the other two arguments to my write, it started working every time.

From the api:



I'm happy it works, but not quite satisfied I understand why. Or why it would be unpredictable -- I could repost the same data multiple times, with an inconsistent result.... weird.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Read the API documentation for the read(byte[]) method of InputStream carefully. Then ask yourself what would happen if that method put 389 bytes into that array. Would writing out 389 bytes be the right thing to do, or would writing out 1024 bytes be the right thing to do?
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38


After the read I'd have 389 meaningful bytes in my 1024 array. With the first form of write, I was writing all 1024 (because bytes.length is 1024 of course), and with the second, I was writing only the number of bytes read into the buffer.

Thanks Paul!
 
wood burning stoves
 
subject: servlet writes file that is sometimes corrupt