Java 下载 HTTP 资源保存到本地
来源:互联网 发布:c语言怎么写驱动 编辑:程序博客网 时间:2024/06/05 10:10
最近做个新功能,需要用到这个功能,测试无误,发上来备忘 :)
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>
* <p>WebSite: http://blog.csdn.net/DavidHsing</p>
*
* @author : DavidHsing <DavidHsing(At)163.com>
* @version : 1.00
* @date : 2007-08-28
* @direction: 下载 HTTP 网络资源保存到本地
******************************************************************************/
import java.io.*;
import java.net.*;
import java.util.*;
//******************************************************************************
public class HttpDownload
...{
//==========================================================================
//public HttpDownload() {}
//==========================================================================
private Vector vRemoteHttpURL = new Vector();
private Vector vLocalSaveFile = new Vector();
//==========================================================================
/** *//**
* 设置代理服务器
* @param String
* @param String
*/
public void 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
*/
public boolean addOneTask(String sRemoteHttpURL, String sLocalSaveFile)
...{
if (sRemoteHttpURL == null || sRemoteHttpURL.trim().equals("") || !sRemoteHttpURL.trim().substring(0, 7).equalsIgnoreCase("http://"))
...{
System.out.println(" @> HttpDownload.addOneTask() : 源地址有误,不是一个有效的 http 地址!");
return false;
}
else
...{
if (sLocalSaveFile == null || sLocalSaveFile.trim().equals(""))
...{
sLocalSaveFile = "./" + sRemoteHttpURL.substring(sRemoteHttpURL.lastIndexOf("/") + 1);
}
vRemoteHttpURL.add(sRemoteHttpURL);
vLocalSaveFile.add(sLocalSaveFile);
}
return true;
}
//==========================================================================
/** *//**
* 清除下载列表
*/
public void clearAllTasks()
...{
vRemoteHttpURL.clear();
vLocalSaveFile.clear();
}
//==========================================================================
/** *//**
* 根据列表下载资源
* @return boolean
*/
public boolean 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;
}
}
return true;
}
//==========================================================================
/** *//**
* 将 HTTP 资源保存为文件
* @param String
* @param String
* @return boolean
*/
private boolean saveToFile(String sRemoteHttpURL, String sLocalSaveFile)
...{
if (sRemoteHttpURL == null || sRemoteHttpURL.trim().equals(""))
...{
System.out.println(" @> HttpDownload.saveToFile() : 要下载的远程资源地址不能为空!");
return false;
}
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 = new byte[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);
return false;
}
return true;
}
//==========================================================================
///**
// * 主方法(用于测试)
// * @param argv String[]
// */
public static void main(String argv[])
...{
HttpDownload tHttpDownload = new HttpDownload();
tHttpDownload.addOneTask("http://www.baidu.com/test.zip", "C:/test.zip");
tHttpDownload.downLoadByList();
tHttpDownload = null;
}
//==========================================================================
} //class HttpDownload end
- Java 下载 HTTP 资源保存到本地
- unity从服务器下载资源并且保存到本地
- 【入门篇】Activiti流程资源文件下载保存到本地
- unity从服务器下载资源并且保存到本地
- Java Http下载文件到本地
- 下载视频文件保存到本地
- java 根据url下载图片保存到本地
- Java 根据url下载图片保存到本地
- java 根据url下载图片保存到本地
- java 中 excel生成并文件下载保存到本地
- java 从服务器下载文件并保存到本地
- php 下载保存文件保存到本地
- QT http练习——创建一个能够下载URL资源的UI程序,并保存到本地指定目录下
- Java通过Http请求下载文本附件到本地
- 将项目中的资源保存到本地
- vc 保存http服务器文件到本地
- Android 下载网络图片保存到本地
- 安卓下载保存到本地(一)
- 要为找工作做准备了
- ADO.NET介绍
- OpenSSL的简单使用
- DB2中通用的存储过程分页程序
- 数据库乱码处理
- Java 下载 HTTP 资源保存到本地
- mobile games
- 用OpenSSL命令行生成证书文件
- MSN在线代码|QQ在线客服代码|SKYPE 贸易通在线代码
- "JAVA内存泄漏"一个永恒的主题
- 动态编译、运行JAVA程序
- Java连接各种数据库的实例
- Delphi 如何实现窗口透明,但是窗口上的文字不透明
- RegisterStartupScript和RegisterClientScriptBlock输出位置的区别