[Java] Download File from URL

In your program, you might want to acquire files from the internet for purposes such as update, additional resources, web image gallery… so here’s a modified version of the codes I found on stackoverflow.com that you can just copy and paste into your Java program. Just supply the URL for path and the file name in filename.

 public static String downloadFile(String path, String filename)
 {


 try {
 URL website = new URL(path);
 ReadableByteChannel rbc = Channels.newChannel(website.openStream());
 FileOutputStream fos = new FileOutputStream(filename);
 fos.getChannel().transferFrom(rbc, 0, 1 << 24);
 } catch (IOException ex) {
 Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
 }

 return path + "............ COMPLETED!";


 }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s