Java's HTTP file queue download

xiaoxiao2021-03-06  51

Java's HTTP file queue download

Download source code

Author: Yang Liao

About the Author

Yang Liao, software engineer, you can get in touch with the author through magicsliao@yahoo.com.cn.

Preface

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 locally, you only click on the user to complete save, if The resources have been divided into several hundred or even thousands of thousands of people, which 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.

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 process of downloading 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. 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 for access:

......

Desturl = "http://www.ebook.com/java/ Network Programming 001.zip";

URL = New URL (DESTURL);

Httpurl = (httpurlconnection) URL.OpenConnection ();

/ / Connect the designated network resources

httpurl.connect ();

// Get network input streams

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

......

3. Agent access

The method of accessing the external network through the proxy server in Java is already a 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. Saving of network resources

In the previous section, we have obtained the input stream of the specified network resource. Next, we must 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 ("" "is getting link [ DestURL "] ... / n is saved as file [" filename "] ");

//save document

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

Download source code HTTPGET.JAVA

Import java.io. *;

Import java.net. *;

Import java.util. *;

/ **

* Title: Personal development API

* Description: Store the specified HTTP network resource in the locality * 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 file fos = new fileoutputstream (filename);

IF (this.debug)

System.out.Println ("" "is getting link [ DestURL "] ... / n is saved 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 ());

}

}

? Source code: import java.io. *;

Import java.net. *;

Import java.util. *;

/ **

* Title: Personal development API

* Description: Store the specified HTTP network resources locally in file form

* 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 ("" "is getting link [ DestURL "] ... / n is saved 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 program 7.zip"); // Start download

Oinstance.downloadBylist ();

}

Catch (Exception Err) {

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

}

}

}

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

New Post(0)