Solved

Downloading image

Posted on 2003-11-24
4
226 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array6 challenfge 6 62
Modeling a class in java 5 31
get weblogic logged in user in java 2 39
eclipse java  build path 6 17
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now