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();
}
/**
* 将HTTP资源另存为文件
*
* @param destUrl String
* @param fileName String
* @throws Exception
*/
public void saveToFile2(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();
// String authString = "username" ":" "password";
String authString = "50301" ":" "88888888";
String auth = "Basic "
new sun.misc.BASE64Encoder().encode(authString.getBytes());
httpUrl.setRequestProperty("Proxy-Authorization", auth);
//连接指定的资源
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);
}
public void setAuthenticator(String uid, String pwd) {
Authenticator.setDefault(new MyAuthenticator());
}
/**
* 主方法(用于测试)
*
* @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.downLoadByList();
oInstance.saveToFile("http://www.ebook.com/java/网络编程001.zip", "./down.zip");
}
catch (Exception err) {
System.out.println(err.getMessage());
}
}
}
上一篇: 多核、超线程CPU时代将成就Java性能之王的地位
下一篇: [转]从Java开源说起
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


