File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending an image (under the HTTP protocol)

 
Thomas Emerst
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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


 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AvailableDoesntDoWhatYouThinkItDoes.
 
Thomas Emerst
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Carey Brown
Ranch Hand
Pie
Posts: 1060
9
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic