import java.io.*;
import java.net.*;
import java.util.*;
public class test {
public final static boolean DEBUG = true;
private static int BUFFER_SIZE = 8096;
private Vector vDownLoad = new Vector();//URL列表
private Vector vFileList = new Vector();//下载后的保存文件名列表
public test() {}
public void resetList() {
vDownLoad.clear();
vFileList.clear();
}
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(err.toString());
System.out.println("资源[" + url + "]下载失败!!!");
}
}
}
if (DEBUG) {
System.out.println("下载完成!!!");
}
}
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;
httpUrl.setRequestMethod("GET");
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 argv String[]
*/
public static void main(String argv[]) {
test oInstance = new test();
try {
String name = URLEncoder.encode("投诉黑名单", "UTF-8");
name=name+"%2020060523.txt";
oInstance.addItem("http://sp.iuni.com.cn/download.jsp?file=投诉黑名单 20060523.txt","20051019.txt");
oInstance.downLoadByList();
}
catch (Exception err) {
System.out.println(err.getMessage());
}
}
}
java_http_down.rar_down JAVA_java http
版权申诉
98 浏览量
2022-09-23
06:35:20
上传
评论
收藏 1KB RAR 举报
alvarocfc
- 粉丝: 112
- 资源: 1万+
最新资源
- STM32H750VBT6单片机最小系统开发板AD设计硬件(原理图+PCB+3D封装库)工程文件.zip
- 基于74LS161+ 74LS192芯片实现倒计时定时器Multisim仿真源文件,Multisim10以上版本可打开运行
- 科大讯飞语音引擎 jar包 demo,科大讯飞语音合成引擎3.0,支持4.0系统以上,文字转语音输出.zip
- Java架构面试笔试专题资料及经验(含答案)SpringBoot面试Linux面试专题及答案 合集.zip
- 头歌c语言实验答案tion-model-for-ne开发笔记
- docker配置使用-model-for-networK开发demo
- docker配置使用vaWeb-mas笔记
- c语言连接两个字符串-mas开发笔记
- 俄罗斯引擎yandex进入x-master 笔记
- 头歌c语言实验答案el-for-network-ids-ma笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈