• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

servlet writes file that is sometimes corrupt

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


 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic