• We just launched and are currently in beta. Join us as we build and grow the community.

Download File from Website using Java

Jaeh01

Social Presence Architect
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
46
Likes
25
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
This is a tutorial in which we will going to create a program that can download an image file or any files in a website using Java.


So, now let's start this tutorial!


1. Open JCreator or NetBeans and make a java program with a file name of imgFromURL.java.

2. Import the following packages:
  1. import

    java.io.*

    ;

    //used to access the DataInputStream and FileOutputStream class that will get and save the file to the directory program
  2. import

    java.net.*

    ;

    //used to access the URL and URL connection class to access websites

3. Initialize your variable in your Main, variable p for byte data type which will save memory in a large array since this data type is 4 times smaller than int data type and variable url for the URL class that will open the world wide web in a specific website as instantiated.

  1. byte

    [

    ]

    b =

    new

    byte

    [

    1

    ]

    ;
  2. URL

    url =

    new

    URL

    (

    "http://www.sourcecodester.com/sites/default/files/print_0.png"

    )

    ;

We access an image file as we have a png extension file from the sourcecodester site.

Now create a variable named urlConnect for the URLConnection class that we will open the connection using the openConnection method of the variable url above. And with the urlConnect variable, have its connect method to perform connectivity to the site. But take note that we will need the internet on this to download the file.

  1. URLConnection

    urlConnect =

    url.openConnection

    (

    )

    ;
  2. urlConnect.connect

    (

    )

    ;

3. Create now a variable di for our DataInputStream class to get the the inputted url from the URLConnection class.

  1. DataInputStream

    di =

    new

    DataInputStream

    (

    urlConnect.getInputStream

    (

    )

    )

    ;

Now, to get the file we will use the FileOutputStream and have it instantiated with the name of the file to be saved. Have this code below:

  1. FileOutputStream

    fo =

    new

    FileOutputStream

    (

    "print_0.png"

    )

    ;

4. To download the file, have this code below using the FileOutputStream, DataInputStream variable and the byte variable, then close the di and fo variable for streaming.

  1. while

    (

    -

    1

    !=

    di.read

    (

    b, 0

    , 1

    )

    )
  2. fo.write

    (

    b, 0

    , 1

    )

    ;
  3. di.close

    (

    )

    ;
  4. fo.close

    (

    )

    ;

Output:
imgurl.png


Here's the full code of this tutorial:

  1. import

    java.io.*

    ;

    //used to access the DataInputStream and FileOutputStream class that will get and save the file to the directory program
  2. import

    java.net.*

    ;

    //used to access the URL and URL connection class to access websites

  3. public

    class

    imgFromURL {
  4. public

    static

    void

    main(

    String

    args[

    ]

    )

    throws

    Exception

    {

  5. byte

    [

    ]

    b =

    new

    byte

    [

    1

    ]

    ;

    //data type to save memory space of the file
  6. URL

    url =

    new

    URL

    (

    "http://www.sourcecodester.com/sites/default/files/print_0.png"

    )

    ;

    // url of the site that we will going to download the image file

  7. URLConnection

    urlConnect =

    url.openConnection

    (

    )

    ;
  8. urlConnect.connect

    (

    )

    ;

    // get the connection to the site


  9. DataInputStream

    di =

    new

    DataInputStream

    (

    urlConnect.getInputStream

    (

    )

    )

    ;

    //have the input to the connection

  10. FileOutputStream

    fo =

    new

    FileOutputStream

    (

    "print_0.png"

    )

    ;

    //download the file and have it named print_0.png

  11. while

    (

    -

    1

    !=

    di.read

    (

    b, 0

    , 1

    )

    )

    //download the image file
  12. fo.write

    (

    b, 0

    , 1

    )

    ;
  13. di.close

    (

    )

    ;

    // close DataInputStream
  14. fo.close

    (

    )

    ;

    // close FileOutputStream
  15. }
  16. }

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware



Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

335,994

336,002

Top