File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Sending an image (under the HTTP protocol) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Sending an image (under the HTTP protocol)" Watch "Sending an image (under the HTTP protocol)" New topic
Author

Sending an image (under the HTTP protocol)

Thomas Emerst
Greenhorn

Joined: Dec 26, 2010
Posts: 7
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


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

AvailableDoesntDoWhatYouThinkItDoes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Thomas Emerst
Greenhorn

Joined: Dec 26, 2010
Posts: 7


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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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

Joined: Dec 26, 2010
Posts: 7
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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.
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 174

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending an image (under the HTTP protocol)