Jump to content

Java - Download files from the web.

- - - - -

  • Please log in to reply
3 replies to this topic

#1
farrell2k

farrell2k

    Learning Programmer

  • Members
  • PipPipPip
  • 60 posts
I skipped the GUI to make the code easier to read on this one.

Find a file somewhere online and instantiate a URL object.

Create a URLConnection Object that represents a connection to the URL.

Create a DataStream Object to handle the incoming Stream of Data.

Create a byte[] array to hold the raw data of the file you want to download, setting the size of the array from the getContentLength() method from the URL connection..

Create a FileOutPutStream object which you will use to write your download to a file on disk.

Create a for loop and use the DataInputStream readByte() method to fill the byte[] array with bytes from the file you're downloading.


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

public class Main {
    public static void main(String[] args) {
        URL url; //represents the location of the file we want to dl.
        URLConnection con;  // represents a connection to the url we want to dl.
        DataInputStream dis;  // input stream that will read data from the file.
        FileOutputStream fos; //used to write data from inut stream to file.
        byte[] fileData;  //byte aray used to hold data from downloaded file.
        try {
            url = new URL("http://icanhascheezburger.files.wordpress.com/2010/06/funny-pictures-cat-is-creepy.jpg");
            con = url.openConnection(); // open the url connection.
            dis = new DataInputStream(con.getInputStream()); // get a data stream from the url connection.
            fileData = new byte[con.getContentLength()]; // determine how many byes the file size is and make array big enough to hold the data
            for (int x = 0; x < fileData.length; x++) { // fill byte array with bytes from the data input stream
                fileData[x] = dis.readByte();
            }
            dis.close(); // close the data input stream
            fos = new FileOutputStream(new File("file.jpg"));  //create an object representing the file we want to save
            fos.write(fileData);  // write out the file we want to save.
            fos.close(); // close the output stream writer
        }
        catch(MalformedURLException m) {
            System.out.println(m);
        }
        catch(IOException io) {
            System.out.println(io);
        }
    }
}

EDIT: Sorry. Fixed a typo in the for loop.

EDIT: Removed fos.flush() before closing the FileOutputStream. You need to flush() only when you're using a buffer.

Edited by farrell2k, 25 June 2010 - 02:11 PM.


#2
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Awesome Job! + REP
Check out my site: www.khaoticirc.net

#3
cycronica

cycronica

    Newbie

  • Members
  • Pip
  • 5 posts
Really like this one. Not yet tried it but seems to be functional code. Could be fun to create a download manager or something similar with this. Thanks

#4
VakhoQ

VakhoQ

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I have writen Download Manager with GUI. You can download the file with many threads (for example if you want to download 'N' mb file, and you type 'M' threads - my code divades the file with 'M' part (each part will be 'N/M' size ) and it downlaods that parts of file together. as a result, it joins all the parts;

Posted Image

I'll translate comments and other words in to the english, and i'll show you my code :-)
GNU/Linux Is the Best.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users