• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet writes file that is sometimes corrupt

 
Mike Mitchell
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the image data coming from? If it's a file upload from the client, then you are most definitely doing it wrong.
 
Mike Mitchell
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic