Java Program To Download A File From Website

           In this tutorial, we are going to write a program in java, that downloads a file from website.I am going to  download, a pdf file, the url for this is
"http://eur-lex.europa.eu/legal-content/BG/TXT/PDF/?uri=OJ:JOL_2015_011_R_0004&from=EN".

Following are the steps to follow.

1. First of all, you have to make an URL object for the above url, for this use URL class. You know, in Object oriented programming, every thing can be represented in classes and objects. Here URL is a class in Java that represents a website url.  
URL url = new URL("http://eur-lex.europa.eu/legal-content/BG/TXT/PDF/?uri=OJ:JOL_2015_011_R_0004&from=EN");

Now this URL object, represents the above url in java programming notation.

2. The next thing to do is, you have to read from that url, for this purpose, for this purpose, call openStream() method, which returns a reference to the InputStream object. The code for this is
InputStream in = url.openStream();

3. Now you are able to read from the above url, now the last task is, you have to save, what ever you are reading to disk. For this purpose, use OutputStream class, like below.
OutputStream fos = new FileOutputStream("output.pdf"); 

The above line of code, saves the pdf file to the current working directory. If you want to save it to any other directory, you just mention correct path before name of pdf, like below.
OutputStream fos = new FileOutputStream("D://output.pdf"); 

4. Calling openStream() method is not enough, it just gives access to the resource, you need to read bytes from the resource, and write to disk just like below.
  int length = -1;

  byte[] buffer = new byte[1024];// buffer for portion of data from connection

  while ((length = in.read(buffer)) > -1) {

          fos.write(buffer, 0, length);

  }

  5. finally close both input and output streams,
    fos.close(); 

    in.close();

6. The complete woking code is

package Test;
import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.MalformedURLException;

import java.net.URL;



public class ReadUrl {

    

    public static void main(String[] args) {

        try {

            URL url = new URL("http://eur-lex.europa.eu/legal-content/BG/TXT/PDF/?uri=OJ:JOL_2015_011_R_0004&from=EN");

            InputStream in = url.openStream();

            OutputStream fos = new FileOutputStream("output.pdf");

            int length = -1;

            byte[] buffer = new byte[1024];

            while((length = in.read(buffer)) != -1) {

                fos.write(buffer, 0, length);

            }            

            

        } catch (MalformedURLException e) {            

            e.printStackTrace();

        } catch (IOException e) {            

            e.printStackTrace();

        }

    }



}

You can also download the source code from here "DownloadFile".

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment