Store the specified HTTP network resources in the locally (download)
Import java.io. *;
Import java.net. *;
Import java.util. *;
/ ** *
Title: API p> *
description: Store the specified HTTP network resources in the field p> *
Copyright: CopyRight (C) 2004 p> *
Company: newsky p> * @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 (); // Save file name list
/ ** * Construction method * / public httpget () {
}
/ ** * Clear download list * / public void resetList () {vdownload.clear (); vfilelist.clear ();
/ ** * Add download list item * * @Param url string * @Param filename string * / public void additem (string url, string filename) {vdownload.add (URL); vfilelist.add (filename);}
/ ** * Download Resource According to List * / Public Void DownloadBylist () {string URL = NULL; String FileName = NULL; // Save the resource for resource for (int i = 0; i Try {Savetofile (URL, FileName);} catch (ioexception err) {if (debug) {system.out.println ("Resource [" URL "] download failed !!!");}}} IF (debug) {system.out.println ("Download Complete !!!"); } / * ** The HTTP resource as a file * * @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; // establish a link url = new uRL (destUrl); httpUrl = (HttpURLConnection) url.openConnection (); / / Connect the specified resource httpurl.connect (); // Get network input stream bis = new bufferedinputstream (httpurl.getinputstream ()); // creation file fos = new fileoutputstream (filename); if (this.debug) System.out .println ("" Getting Link [" DestURL "] content ... / n Save it as file [" FileName "] "); / / Save the file while ((size = bis.read (buf))! = -1) fos.write (buf, 0, size); fos.close (); bis.close (); httpurl.disconnect (); } / ** * Setup 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 download list (here users can write their own code to add download list) Oinstance.AddItem ("http://www.ebook.com/java/ network programming 001. ZIP "," ./ Network Program 1.zip "); OInstance.AddItem (" http://www.ebook.com/java/ Network Programming 002.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 program 004.zip "," ./ Network programming 4.zip "); oinstance.additem (" http://www.ebook.com/java/ network programming 005.zip "," ./ Network program 5. Zip "); OInstance.Additem (" http://www.ebook.com/java/ network program 006.zip "," ./ network programming 6.zip "); oinstance.addItem (" http: // www. eBook.com/java/ Network Programming 007.zip "," ./ Network Program 7.zip "); // Start downloading oinstance.downloadBylist ();} catch (exception err) {system.out.println (Err.getMessage ());}} }