• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sending an image (under the HTTP protocol)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've created this simple HTTP web-server. For now, it's simply sending files on response, so I'm obviously able to send HTML files and such.
I'm willing to implement image-sending, so I that I could send images as-well, but I've encountered a problem.

When the server receives a request for an image file, it reads up the file.



This function reads the requested file and returns a byte array of the file.
We process the returned array, and store it in the String "temp", after casting each byte to char:



Then we append it to our HTTP message and flush it to the client:



Using the browser, I get a "The image “http://localhost/error_0x0.PNG” cannot be displayed, because it contains errors." error with Firefox. The file's obviously corrupt.

I decided to compare the image I receive from the server I made, and the original one, and they're different:


I must be reading the file wrong, or converting it wrong somehow..
What am I doing wrong?

Thanks


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AvailableDoesntDoWhatYouThinkItDoes.
 
Thomas Emerst
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:AvailableDoesntDoWhatYouThinkItDoes.



How is this related, may I ask?
I also forgot to add that the file received from the server and the original file both end up being 7,350 bytes (7.17 kbytes) long, so it's safe to assume it was transferred completely.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case it probably won't make a difference, but if the source is some remote URL then available() may return 0 while there still is data left. It's best to avoid using available().

However, that's not the cause. This is:
Not all bytes can be directly converted to characters. An image is binary data, not text data. It needs to be properly encoded.

I'll move this thread to our I/O forum.
 
Thomas Emerst
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:In this case it probably won't make a difference, but if the source is some remote URL then available() may return 0 while there still is data left. It's best to avoid using available().

However, that's not the cause. This is:
Not all bytes can be directly converted to characters. An image is binary data, not text data. It needs to be properly encoded.

I'll move this thread to our I/O forum.



Thanks for reinforcing my gut feeling. It may sound silly, but I'm not sure how am I supposed to convert(/typecast) the bytes, and to what type. Could you please advise me regarding this issue?

Thanks, and again, sorry for the bother
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I don't know enough about the raw HTTP protocol to answer that question...

I just did a test for the Ranch's moose, and it's returning plain ol' binary in the HTTP response. That said, it probably doesn't use (the equivalent of) a Writer to return the data but (the equivalent of) an OutputStream instead.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As mentioned, available() is unreliable. You'll need to put the read() call in a loop. You'll need to make a byte[] that will hold the entire image file contents. You can output binary data in HTTP but BufferredWriter is not the tool for the job, it expects characters and you need to send it a byte[]. You'll have to put the length of the byte[] into the Content-Length value of the header. The next question is what flavor of data is the receiving HTTP program expecting?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic