Implement HTTP file queue download with Java

xiaoxiao2021-03-06  101

Prospects Many users may encounter such situations: I found a good resource on the website, but this resource is divided into many files. If you want to save it to your local, you only click on the user to complete the save, If the resource is divided into a few hundred or even thousands of thousands, it is a disaster. When there are many resources on the Internet, it is a certain rule when it is stored in multiple files. For this reason, we can use the program to complete the full download of this resource. 1. Basic knowledge On the Internet, we have to download a resource on the website, we will get a URL (Uniform Resource Locator), which is a description of a server resource location, the download process is always as follows:

Step 1: Client initiates a connection request A URL Step 2: The server parses the URL and returns the specified resource to the client step 3: The client receives the input stream, save the content in the stream to the file

2. The establishment of network connection Java provides an API for URL access and a large number of stream operations. We can easily complete access to resources on the network. The following code segments have completed access to a website:

... destURL = "http://www.ebook.com/java/ network program 001.zip"; URL = New URL (DESTURL); httpurl = (httpurlconnection) URL.OpenConnection (); // connection Specified network resources httpurl.connect (); // Get network input stream bis = new bufferedInputStream (httpurl.getinputstream ()); ......

3. The method of accessing the external network through the proxy server in Java is already the secret of the world. There is no more described here, and the Java code accessed is as follows:

// Set proxy server system.getProperties (). Put ("proxyset", "true"); system.getProperties (). Put ("proxyhost", "10.154.134.110"); system.getProperties (). Put (" Proxyport "," 8080 ");

4. Save of network resources In the previous section, we have obtained the input stream of the specified network resource, and then we have to complete the content in the input stream, and save it in the file. Sample code:

... fos = new fileoutputstream (filename); if (this.debug) System.out.println ("" Getting Link [ DestURL "] content ... / n Save it as a file [ " FileName "] "); // Save the file while ((size = bis.read (buf))! = -1) fos.write (buf, 0, size); ......

The sample code above saves the content of the network resource to the local specified file. 5. Code list

Import java.io. *;

Import java.net. *;

Import java.util. *;

/ **

*

Title: API for personal development

*

Description: Store the specified HTTP network resources in the locally in the field

*

Copyright: Copyright (C) 2004

*

Company: newsky * @Author Magicliao

* @version 1.0

* /

Public class httpget {

Public final static boolean debug = true; // debugging

Private static int buffer_size = 8096; // buffer size

Private vector vdownload = new vector (); // URL list

Private vector vfilelist = new vector (); // Download the saved file name list

/ **

* Construction method

* /

Public httpget () {

}

/ **

* Clear download list

* /

Public void resetlist () {

vdownload.clear ();

vfilelist.clear ();

}

/ **

* Add a download list item

*

* @Param URL String

* @Param FileName String

* /

Public void additem (string url, string filename) {

vDownload.add (URL);

vfilelist.add (filename);

}

/ **

* Download resources according to the list

* /

Public void downloadbylist () {

String Url = NULL;

String filename = NULL;

/ / Save resources in list order

For (int i = 0; i

URL = (String) vdownload.get (i);

FileName = (string) vFileList.get (i);

Try {

Savetofile (URL, FileName);

}

Catch (IOException Err) {

IF (debug) {

System.out.println ("Resource [" URL "] download failed !!!");

}

}

}

IF (debug) {

System.out.println ("Download Complete !!!");

}

}

/ **

* Save HTTP resources as file

*

* @Param Desturl String

* @Param FileName String

* @Throws Exception

* /

Public void savetofile (string desturl, string filename) throws oException {

Fileoutputstream fos = NULL;

BufferedInputStream Bis = NULL;

HTTPURLCONNECTION httpurl = null;

URL URL = NULL;

BYTE [] BUF = New byte [buffer_size];

INT size = 0;

// Establish a link

URL = New URL (DESTURL);

Httpurl = (httpurlconnection) URL.OpenConnection ();

// Connect the specified resource

httpurl.connect ();

// Get network input streams

BIS = New BufferedInputStream (httpurl.getinputStream ());

// Establish a file

FOS = New FileOutputStream (filename);

If (this.debug) System.out.println ("Getting the link [ DestURL "] content ... / n Save it as file [" filename "] ");

//save document

While (size = bis.read (buf))! = -1)

Fos.Write (buf, 0, size);

Fos.close ();

bis.close ();

Httpurl.disconnect ();

}

/ **

* Set proxy server

*

* @Param Proxy String

* @Param Proxyport String

* /

Public void setProxyserver (String Proxy, String Proxyport) {

// Set proxy server

System.getProperties (). Put ("Proxyset", "True");

System.getProperties (). PUT ("proxyhost", proxy;

System.getProperties (). PUT ("proxyport", proxyport;

}

/ **

* Set the authentication user name and password

*

* @Param Uid String

* @Param PWD STRING

* /

Public void setauthenticator (String Uid, String PWD) {

Authenticator.SetDefault (New MyAuthenTicator (UID, PWD));

}

/ **

* Main method (for testing)

*

* @Param Argv String []

* /

Public static void main (String Argv []) {

HTTPGET OINSTANCE = New httpget ();

Try {

/ / Add a list of downloads (here users can write their own code to increase the download list)

Oinstance.addItem ("http://www.ebook.com/java/ network programming 001.zip", "./ network programming 1.zip");

Oinstance.addItem ("http://www.ebook.com/java/ network program 002.zip", "./ Network programming 2.zip");

Oinstance.additem ("http://www.ebook.com/java/ network program 003.zip", "./ Network programming 3.zip");

Oinstance.additem ("http://www.ebook.com/java/ network programming 004.zip", "./ Network programming 4.zip");

Oinstance.Additem ("http://www.ebook.com/java/ network program 005.zip", "./ Network programming 5.zip");

Oinstance.additem ("http://www.ebook.com/java/ network programming 006.zip", "./ Network program 6.zip");

Oinstance.addItem ("http://www.ebook.com/java/ network programming 007.zip", "./ Network programming 7.zip");

//start download

Oinstance.downloadBylist ();

}

Catch (Exception Err) {

System.out.println (Err.getMessage ());

}

}

}

Download source code

转载请注明原文地址:https://www.9cbs.com/read-105464.html

New Post(0)