代码清单
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());
}
}
}