URL image download problem

B

ByteCoder

I just created the code below to download a file from a URL. Text/HTML
files work fine, but images don't show up properly in an image viewer.
The weird thing is that the correct number of bytes (from the google
logo) are counted. OutputStreamWriter is flushed etc.

I think it's some sort of byte to int conversion problem, but I have no
idea how to do this differently.

Any help would be greatly appreciated!


---working method:---

private void downloadURL() {
try {
// Create the URL
URL u = new URL
("http://www.google.com/intl/en/images/logo.gif");
// Save the file the URL points to to this file
File file = new File(System.getProperty("user.home") +
File.separator + "logo.gif");

// Create a connection with the URL
InputStream input = u.openStream();
input = new BufferedInputStream(input);
Reader reader = new InputStreamReader(input);

// Create the OutputStream and Writer
FileOutputStream output = new FileOutputStream(file);
Writer writer = new OutputStreamWriter(output);

// Write the received bytes to the file.
int numberOfBytes = 0;
int c;
while((c = reader.read()) != -1) {
writer.write(c);
numberOfBytes++;
}
System.out.println("File size: " + numberOfBytes + " byte
(s).");
// Flush the writer!
writer.flush();

// Close connections.
reader.close();
writer.close();
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
}

---end working method---
 
R

Roland

I just created the code below to download a file from a URL. Text/HTML
files work fine, but images don't show up properly in an image viewer.
The weird thing is that the correct number of bytes (from the google
logo) are counted. OutputStreamWriter is flushed etc.

I think it's some sort of byte to int conversion problem, but I have no
idea how to do this differently.

Any help would be greatly appreciated!


---working method:---
private void downloadURL() {
try {
// Create the URL
URL u = new URL("http://www.google.com/intl/en/images/logo.gif");
// Save the file the URL points to to this file
File file = new File(System.getProperty("user.home")
+ File.separator + "logo.gif");

// Create a connection with the URL
InputStream input = u.openStream();
input = new BufferedInputStream(input);

// Create the OutputStream and Writer
FileOutputStream output = new FileOutputStream(file);

// Write the received bytes to the file.
int numberOfBytes = 0;
int b;
while ((b = input.read()) != -1) {
output.write(b);
numberOfBytes++;
}
System.out.println("File size: " + numberOfBytes
+ " byte(s).");

// Close connections.
input.close();
output.close();
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
}
---end working method---


--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
R

Roland

What I want to add: don't use Readers and Writers when you are handling
binary data, like that of the image. Readers and Writers are for
reading/writing *character* data.
An InputStreamReader converts a sequence of *bytes* (i.e *one or more*
bytes) into a character, using a specified character encoding (if not
specified, the platform's default char encoding may be used). The image
data may contain sequences of bytes that are not recognized or invalid
to the char encoding that's being used. In that case the
InputStreamReader drops those bytes or replaces them, for instance by
the question-mark character '?'.
Similar things apply to OutputStreamWriter: it converts a character to a
sequence of *one or more* bytes, depending on the char encoding that's
being used.
And that explains how your image got garbled.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
R

Roland

Thanks!
I guess it was the reader and writer that caused the problems?
Yep, I pressed the send button to early. See my other reply for the
explanation on Readers/Writers.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
B

ByteCoder

What I want to add: don't use Readers and Writers when you are
handling binary data, like that of the image. Readers and Writers are
for reading/writing *character* data.
An InputStreamReader converts a sequence of *bytes* (i.e *one or more*
bytes) into a character, using a specified character encoding (if not
specified, the platform's default char encoding may be used). The
image data may contain sequences of bytes that are not recognized or
invalid to the char encoding that's being used. In that case the
InputStreamReader drops those bytes or replaces them, for instance by
the question-mark character '?'.
Similar things apply to OutputStreamWriter: it converts a character to
a sequence of *one or more* bytes, depending on the char encoding
that's being used.
And that explains how your image got garbled.

Thank you very much. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top