The document Java HTTP download queue
Abstract: The Java HTTP download documents queue
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="730" border="0"> <tr> <td> </ td> </ tr> </ table>
Import java.io. *;
Import java.net .*;
Import java.util .*;
/ **
*
Title: Personal development of the API
*
Description: HTTP will be designated to local network resources in the form of document storage
*
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 ();// after downloading the file name list
/ **
* Construction
* /
Public HttpGet () (
)
/ **
* Removal Download List
* /
Public void resetList () (
VDownLoad.clear ();
VFileList.clear ();
)
/ **
* Download List of
*
* @ Param url String
* @ Param filename String
* /
Public void addItem (String url, String filename) (
VDownLoad.add (url);
VFileList.add (filename);
)
/ **
* Based on the download list of resources
* /
Public void downLoadByList () (
String url = null;
String filename = null;
/ / Resource preservation order on List
For (int i = 0; i <vDownLoad.size (); i + +) (
Url = (String) vDownLoad.get (i);
Filename = (String) vFileList.get (i);
Try (
SaveToFile (url, filename);
)
Catch (IOException err) (
If (DEBUG) (
System.out.println ( "resources [" + url + "] Download failure !!!");
)
)
)
If (DEBUG) (
System.out.println ( "download !!!");
)
)
/ **
* Save the document HTTP resources
*
* @ Param destUrl String
* @ Param fileName String
* @ Throws Exception
* /
Public void saveToFile (String destUrl, String fileName) throws IOException (
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
Byte [] buf = new byte [BUFFER_SIZE];
Int size = 0;
/ / Links
Url = new URL (destUrl);
HttpUrl = (HttpURLConnection) url.openConnection ();
/ / Connect designated resources
HttpUrl.connect ();
/ / Get input flow network
Bis = new BufferedInputStream (httpUrl.getInputStream ());
/ / Create documents
Fos = new FileOutputStream (fileName);
If (this.DEBUG)
System.out.println ( "access links are [" + + destUrl "] … \ n will be preserved for its paper [" + + "]"); fileName
/ / Save the file
While ((size = bis.read (buf))! = -1)
Fos.write (buf, 0, size);
Fos.close ();
Bis.close ();
HttpUrl.disconnect ();
)
/ **
* Set up a 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 up user name and password authentication
*
* @ Param uid String
* @ Param pwd String
* /
Public void setAuthenticator (String uid, String pwd) (
/ / Authenticator.setDefault (new MyAuthenticator (uid, pwd));
)
/ **
* The main method (for testing)
*
* @ Param argv String []
* /
Public static void main (String argv []) (
HttpGet oInstance = new HttpGet ();
Try (
/ / Increase download list (here users can write their own code to increase the download list), to save downloaded files in the current directory.
OInstance.addItem ( "http://java.dhcn.net:8080/softdownload/dir1/dir5/10.zip ","./ Network Programming 1. Zip");
OInstance.addItem ( "http://java.dhcn.net:8080/softdownload/dir1/dir5/10233.zip ","./ Network Programming 2. Zip");
/ / OInstance.addItem ( "http://www.ebook.com/java/ Network Programming 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 Programming 005. Zip ","./ network programming 5. Zip");
/ / OInstance.addItem ( "http://www.ebook.com/java/ Network Programming 006. Zip ","./ network programming 6. Zip");
/ / OInstance.addItem ( "http://www.ebook.com/java/ Network Programming 007. Zip ","./ Network Programming 7. Zip");
/ / Start the download
OInstance.downLoadByList ();
)
Catch (Exception err) (
System.out.println (err.getMessage ());
)
)
)
Running Results:
C: \ java> javac HttpGet.java
C: \ java> java HttpGet
Access links are [http://java.dhcn.net:8080/softdownload/dir1/dir5/10.zip] …
For the preservation of their documents [. / Network Programming 1. Zip]
Access links are [http://java.dhcn.net:8080/softdownload/dir1/dir5/10233.zip] …
For the preservation of their documents [. / Network Programming 2. Zip]
Download!
C: \ java>
</ Td> </ tr> <tr>
↑ Back
Tags: java download






