?
Solved

Downloading image

Posted on 2003-11-24
4
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

719 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