Java 下載 HTTP 資源儲存到本地
阿新 • • 發佈:2019-01-07
最近做個新功能,需要用到這個功能,測試無誤,發上來備忘 :)
package com.xxx.utility;
/*******************************************************************************
* <p>Title: xxx</p>
* <p>Description: xxx</p>
* <p>Copyright: Copyright (c) 2006 DavidHsing All Rights Reserved</p>
* <p>Company: xxx</p>
*
* @author : DavidHsing <DavidHsing(At)163.com>
* @version : 1.00
* @date : 2007-08-28
* @direction: 下載 HTTP 網路資源儲存到本地
******************************************************************************/
import java.io.*;
import java.net.*;
//******************************************************************************
publicclass HttpDownload
{
//==========================================================================
//public HttpDownload() {}
//==========================================================================
private Vector vRemoteHttpURL =new Vector();
private Vector vLocalSaveFile =new Vector();
//==========================================================================
/**
* 設定代理伺服器
* @param String
* @param String
*/
publicvoid setProxy(String sProxyHost, String sProxyPort)
{
if (sProxyHost !=null&&!sProxyHost.trim().equals(""))
{
if (sProxyPort ==null|| sProxyPort.trim().equals(""))
{
sProxyPort ="80";
}
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", sProxyHost);
System.getProperties().put("proxyPort", sProxyPort);
}
}
//==========================================================================
/**
* 新增一個下載任務
* @param String
* @param String
* @return boolean
*/
publicboolean addOneTask(String sRemoteHttpURL, String sLocalSaveFile)
{
if (sRemoteHttpURL ==null|| sRemoteHttpURL.trim().equals("") ||!sRemoteHttpURL.trim().substring(0, 7).equalsIgnoreCase("http://"))
{
System.out.println(" @> HttpDownload.addOneTask() : 源地址有誤,不是一個有效的 http 地址!");
returnfalse;
}
else
{
if (sLocalSaveFile ==null|| sLocalSaveFile.trim().equals(""))
{
sLocalSaveFile ="./"+ sRemoteHttpURL.substring(sRemoteHttpURL.lastIndexOf("/") +1);
}
vRemoteHttpURL.add(sRemoteHttpURL);
vLocalSaveFile.add(sLocalSaveFile);
}
returntrue;
}
//==========================================================================
/**
* 清除下載列表
*/
publicvoid clearAllTasks()
{
vRemoteHttpURL.clear();
vLocalSaveFile.clear();
}
//==========================================================================
/**
* 根據列表下載資源
* @return boolean
*/
publicboolean downLoadByList()
{
for (int i =0; i < vRemoteHttpURL.size(); i++)
{
String sRemoteHttpURL = (String)vRemoteHttpURL.get(i);
String sLocalSaveFile = (String)vLocalSaveFile.get(i);
if (!saveToFile(sRemoteHttpURL, sLocalSaveFile))
{
System.out.println(" @> HttpDownload.downLoadByList() : 下載遠端資源時出現異常!");
//return false;
}
}
returntrue;
}
//==========================================================================
/**
* 將 HTTP 資源儲存為檔案
* @param String
* @param String
* @return boolean
*/
privateboolean saveToFile(String sRemoteHttpURL, String sLocalSaveFile)
{
if (sRemoteHttpURL ==null|| sRemoteHttpURL.trim().equals(""))
{
System.out.println(" @> HttpDownload.saveToFile() : 要下載的遠端資源地址不能為空!");
returnfalse;
}
try
{
URL tURL =new URL(sRemoteHttpURL);
HttpURLConnection tHttpURLConnection = (HttpURLConnection)tURL.openConnection();
tHttpURLConnection.connect();
BufferedInputStream tBufferedInputStream =new BufferedInputStream(tHttpURLConnection.getInputStream());
FileOutputStream tFileOutputStream =new FileOutputStream(sLocalSaveFile);
int nBufferSize =1024*5;
byte[] bufContent =newbyte[nBufferSize];
int nContentSize =0;
while ((nContentSize = tBufferedInputStream.read(bufContent)) !=-1)
{
tFileOutputStream.write(bufContent, 0, nContentSize);
}
tFileOutputStream.close();
tBufferedInputStream.close();
tHttpURLConnection.disconnect();
tURL =null;
tHttpURLConnection =null;
tBufferedInputStream =null;
tFileOutputStream =null;
}
catch (Exception ex)
{
System.out.println(" @> HttpDownload.saveToFile() : 下載遠端資源時出現異常!");
System.out.println(" 遠端地址:"+ sRemoteHttpURL);
System.out.println(" 本地路徑:"+ sLocalSaveFile);
returnfalse;
}