import java.io.*;import java.net.*;import java.util.*;/** * <p>Title: 個人開發的API</p> * <p>Description: 將指定的HTTP網絡資源在本地以文件形式存放</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: NewSky</p> * @author MagicLiao * @version 1.0 */public class HttpGet { public final static boolean DEBUG = true;//調試用 private static int BUFFER_SIZE = 8096;//緩沖區大小 private Vector vDownLoad = new Vector();//URL列表 private Vector vFileList = new Vector();//下載后的保存文件名列表 /** * 構造方法 */ public HttpGet() { } /** * 清除下載列表 */ public void resetList() { vDownLoad.clear(); vFileList.clear(); } /** * 增加下載列表項 * * @param url String * @param filename String */ public void addItem(String url, String filename) { vDownLoad.add(url); vFileList.add(filename); } /** * 根據列表下載資源 */ public void downLoadByList() { String url = null; String filename = null;//按列表順序保存資源 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("資源[" + url + "]下載失敗!!!");}} } if (DEBUG) {System.out.println("下載完成!!!"); } } /** * 將HTTP資源另存為文件 * * @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;//建立鏈接 url = new URL(destUrl); httpUrl = (HttpURLConnection) url.openConnection(); //連接指定的資源 httpUrl.connect(); //獲取網絡輸入流 bis = new BufferedInputStream(httpUrl.getInputStream()); //建立文件 fos = new FileOutputStream(fileName); if (this.DEBUG) System.out.println("正在獲取鏈接[" + destUrl + "]的內容.../n將其保存為文件[" + fileName + "]"); //保存文件 while ( (size = bis.read(buf)) != -1)fos.write(buf, 0, size);fos.close(); bis.close(); httpUrl.disconnect(); } /** * 設置代理服務器 * * @param proxy String * @param proxyPort String */ public void setProxyServer(String proxy, String proxyPort) { //設置代理服務器 System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", proxy); System.getProperties().put("proxyPort", proxyPort); } /** * 設置認證用戶名與密碼 * * @param uid String * @param pwd String */ public void setAuthenticator(String uid, String pwd) { Authenticator.setDefault(new MyAuthenticator(uid, pwd)); } /** * 主方法(用于測試) * * @param argv String[] */ public static void main(String argv[]) { HttpGet oInstance = new HttpGet();try {//增加下載列表(此處用戶可以寫入自己代碼來增加下載列表)oInstance.addItem("http://www.ebook.com/java/網絡編程001.zip","./網絡編程1.zip");oInstance.addItem("http://www.ebook.com/java/網絡編程002.zip","./網絡編程2.zip");oInstance.addItem("http://www.ebook.com/java/網絡編程003.zip","./網絡編程3.zip");oInstance.addItem("http://www.ebook.com/java/網絡編程004.zip","./網絡編程4.zip");oInstance.addItem("http://www.ebook.com/java/網絡編程005.zip","./網絡編程5.zip");oInstance.addItem("http://www.ebook.com/java/網絡編程006.zip","./網絡編程6.zip");oInstance.addItem("http://www.ebook.com/java/網絡編程007.zip","./網絡編程7.zip");//開始下載oInstance.downLoadByList(); } catch (Exception err) {System.out.println(err.getMessage()); } }}
|
新聞熱點
疑難解答