Forums Register Login

Writing an Image Into a Local File from a Socket Stream Without Using HTTP-related Classes

+Pie Number of slices to send: Send
Hello!

Obviously, since I can't use an HTTP-related class like any sane programmer would do, this is an assignment.

So I'm given a URL that directly links to an image. My task is to download that image to a local file. I've written a class for it that does the job of separating the HTTP header from the rest of the stream, and writes the file. I've compared normally-downloaded image file with the one I've downloaded using this class, and size-wise they have a very small difference. For some reason, however, they are not identical and the one that I download through Java does not open. So far I wasn't able to figure out the problem.

The biggest problem is that I need to separate the header from the rest of the response, and outside of using a BufferedReader I see no other way of doing it without having to write temporary files and removing the header manually.

All of the StackOverflow answers to similiar questions only provide either methods that use HTTP-related classes (easy as pie) or methods that read the entire byte stream and don't separate the header.

Here's my code:

+Pie Number of slices to send: Send
Too difficult for “beginning”. Moving discussion.

I think you will find the Java Tutorials similarly of no help because they will use HTML related classes too. Start by seeing what you can read from the socket, and print it to the command line/terminal.
+Pie Number of slices to send: Send
I've already done that. The code that I've provided in my OP perfectly separates the header from the rest of the stream. I think the problem lies somewhere in the fact that the BufferedReader is reading into a String, and then in turn that String gets turned into a byte array to be fed to the FileOutputStream. I have a feeling something happens in that conversion of Byte -> String - > Byte that messes up the image. The problem is that if I use a normal reader instead of a buffered reader I have no way to separate the header, since normal readers only read byte-by-byte.
+Pie Number of slices to send: Send
This is where I ought to say, “Don't know.”

So your file consists partially of text (the header) and partially of binary data (the image).
A file reader or similar reads char by char (I think) and returns them as ints. A buffered reader converts a series of such ints into a String (probably converting them back to chars in the process).
A data stream can read your binary data.

Is there any way you can read the header, close the buffered reader, work out an offset from the header's length, and start again with an input stream to read the binary data?

Anybody else?
+Pie Number of slices to send: Send
I've managed to crack this puzzle! I'm not sure if this is the optimal solution, but it works.

+Pie Number of slices to send: Send
Well done

Don't call close() on your streams. Use try‑with‑resources (Java7+) instead.
What does .*/ mean in a regular expression?
Straws are for suckers. Now suck on this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1664 times.
Similar Threads
Send a String to Server and server send it somewhere on network
HTTP Range request
write an object to tomcat server from http socket client
trouble retrieving html of all web sites
Error calling Servlet from java file, while Servlet downloading file from website
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:49:41.