Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Io URL file Download and Save in the Local Directory

Related Tutorials


 
 

Share on Google+Share on Google+

URL file Download and Save in the Local Directory

Advertisement
This Program file download from URL and save this Url File in the specified directory.

URL file Download and Save in the Local Directory

     

This Program file download  from  URL and save this Url File in the specified directory. This program specifies the directory path where the files to be stored  as first command line argument and second command line arguments specifies URL File to be stored. Java creates link between Url and Java application.Using the openConnection() method creates URLConnection object. This connection  read the data using InputStream and FileOutputStream write the data to the local file in a specified directory.  

Source Code : "UrlDownload.java" 

import java.io.*;
import java.net.*;

public class UrlDownload {
final static int size=1024;
public static void
fileUrl(String fAddress, String
localFileName, String destinationDir) {
OutputStream outStream = null;
URLConnection  uCon = null;

InputStream is = null;
try {
URL Url;
byte[] buf;
int ByteRead,ByteWritten=0;
Url= new URL(fAddress);
outStream = new BufferedOutputStream(new
FileOutputStream(destinationDir+"\\"+localFileName));

uCon = Url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((ByteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, ByteRead);
ByteWritten += ByteRead;
}
System.out.println("Downloaded Successfully.");
System.out.println
(
"File name:\""+localFileName+ "\"\nNo ofbytes :" + ByteWritten);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
outStream.close();
}
catch (IOException e) {
e.printStackTrace();
}}}
public static void 
fileDownload
(String fAddress, String destinationDir)
{
 
  int slashIndex =fAddress.lastIndexOf('/');
int periodIndex =fAddress.lastIndexOf('.');

String fileName=fAddress.substring(slashIndex + 1);

if (periodIndex >=&&  slashIndex >= 
&& slashIndex < fAddress.length
()-1)
{
fileUrl(fAddress,fileName,destinationDir);
}
else
{
System.err.println("path or file name.");
}}
public static void main(String[] args)
{

if(args.length==2)
{
for (int i = 1; i < args.length; i++) {
fileDownload(args[i],args[0]);
}
}
else{
  
}
}
}



Download the application
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 19, 2008

Related Tutorials

Discuss: URL file Download and Save in the Local Directory   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:7
Abdul Hameed Basha
November 3, 2011
Good Example...

Simple and need code works fine... Thanks & Regards Abdul Hameed Basha
chf
December 15, 2011
thanks

thanks for sharing this sample
waynegomez
May 15, 2012
how?

so how do you use this?
Bharat
June 8, 2012
What a gr8 logic.

for (int i = 1; i < args.length; i++) { fileDownload(args[i],args[0]); } Does not understand why for loop is required when simple is enough.
hilda
August 22, 2012
plzzzzzzz

donwload
LeRoy
August 29, 2012
Call

Im just confused in What do we put in the parameters , once we have called the class Thank you LeRoy
Arunkumar
September 4, 2013
Struts

it's possible to initiate the application at client side
DMCA.com