Solved

Downloading image

Posted on 2003-11-24
4
257 Views
Last Modified: 2012-05-04
Hi!

I am trying to write a function that download image from url to my computer’s specific local directory.
This is a command-line application. Please show me an example.

Thanks,

0
Comment
Question by:dkim18
  • 3
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9814893
URL url = new URL(text);
URLConnection urlConnection = url.openConnection();
InputStream is = urlConnection.getInputStream();
FileOutputStream file = new FileOutputStream(filename);

int len;
byte[] buf = new byte[256];
while ((len = is.read(buf))>=0)
{
  file.write(buf, 0, len);
}
is.close();
file.close();
0
 

Author Comment

by:dkim18
ID: 9845178
I read  API for java.io.InputStream read(byte[]) method and java.io.FileOutputStream write(byte[], int off, int len).
The read(byte[]) method returns an integer that tells me how many bytes were actually read in. So, I think I should only write that many bytes out. Doesn't your code above only work if the incoming file's byte count is evenly divisible by 256?
0
 
LVL 92

Expert Comment

by:objects
ID: 9846430
the code above does only write the number of bytes read, where len is the number of bytes:

file.write(buf, 0, len);

0
 
LVL 92

Expert Comment

by:objects
ID: 9893343
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate jars 4 57
Eclipse Java import and method not resolved 4 73
Java 8 to Java 6 8 32
Setup GlassFish 4 34
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question