package org.atomic.iptv.download;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.atomic.iptv.Constants;
public class Menuloader implements Runnable {
private String filePath = null;
private List<String> fileList = null;
private int threadNum = 1;
private static int threadID = 1;
private boolean speaker = false;
private String url;
private String encode;
public Menuloader(List<String> fileList) {
Constants constants = Constants.getInstance();
this.filePath = constants.getFilePath();
this.fileList = fileList;
this.speaker = constants.isSpeaker();
this.url = constants.getUrl();
this.threadNum = constants.getThreadNum();
encode = getEncode(url);
filePath = preOP(preOP(filePath,"\\") + encode,"\\");
}
public Menuloader(Constants constants, List<String> fileList) {
this.filePath = constants.getFilePath();
this.fileList = fileList;
this.speaker = constants.isSpeaker();
this.url = constants.getUrl();
this.threadNum = constants.getThreadNum();
encode = getEncode(url);
filePath = preOP(preOP(filePath,"\\") + encode,"\\");
}
public void run() {
if (fileList != null) {
try {
downloadFiles();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void downloadFiles() throws IOException {
int id = threadID++;
if (speaker)
System.out.println("Thread " + id + " is starting!");
createFile(filePath);
try {
for (String name : fileList) {
downloadFile(name);
}
} catch (ConnectException e) {
System.out.println("Connection time out");
return;
}
if (speaker)
System.out.println("Thread " + id + " has finished!");
}
public void mutiDownLoad(int threadNum) {
this.threadNum = threadNum;
if (fileList != null) {
mutiDownLoad(fileList);
}
}
public void mutiDownLoad() {
if (fileList != null) {
mutiDownLoad(fileList);
}
}
private void createFile(String fileFath) throws IOException {
File file = new File(fileFath);
if (!file.exists()) {
file.mkdir();
}
}
/**
* @param name
* @throws IOException
*/
private void downloadFile(String name) throws IOException {
url = preOP(url,"/");
String urlAddress = url + name + ".xml";
URL url = new URL(urlAddress);
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(url.openStream(),
encode));
} catch (FileNotFoundException e) {
if (speaker)
System.out.println("The file:" + name + ".xml is not existed!");
return;
}
File file = new File(filePath + name + ".xml");
if (!file.exists())
file.createNewFile();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), encode));
StringBuilder str = new StringBuilder();
String temp = null;
while ((temp = in.readLine()) != null) {
str.append(temp);
}
out.write(str.toString());
in.close();
out.close();
if (speaker)
System.out.println(filePath + name + " succeed!");
}
private void mutiDownLoad(List<String> fileList) {
int averListSize = 0;
int fromIndex = 0;
int toIndex = 0;
if (fileList.size() / threadNum < 5) {
threadNum = fileList.size() / 5;
averListSize = 5;
} else {
averListSize = fileList.size() / threadNum;
}
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < threadNum; i++) {
toIndex = fromIndex + averListSize;
exec.execute(new Menuloader(fileList.subList(fromIndex, toIndex)));
fromIndex = toIndex;
}
if (fromIndex < fileList.size())
exec.execute(new Menuloader(fileList.subList(fromIndex, fileList
.size())));
exec.shutdown();
}
private String getEncode(String url) {
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
int index = url.lastIndexOf("/");
return url.substring(index + 1, url.length());
}
private String preOP(String url, String part) {
if (!url.endsWith(part)) {
url += part;
}
return url;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
多线程下载的例子很好的
共17个文件
class:9个
java:4个
project:1个
需积分: 0 12 下载量 163 浏览量
2008-11-12
09:22:53
上传
评论
收藏 649KB RAR 举报
温馨提示
呵呵 很好的例子 呵呵 很好的例子 呵呵 很好的例子 呵呵 很好的例子
资源详情
资源评论
资源推荐
收起资源包目录
123.rar (17个子文件)
123
.project 721B
bin
org
atomic
iptv
dao
UUseeIDDAO.class 3KB
Constants.class 2KB
ProgramMenuLoader.class 2KB
download
Menuloader.class 6KB
src
org
atomic
iptv
dao
UUseeIDDAO.java 2KB
UUseeIDDAO.class 3KB
test.class 843B
Constants.class 2KB
Constants.java 2KB
ProgramMenuLoader.java 920B
ProgramMenuLoader.class 2KB
download
Menuloader.class 6KB
Menuloader.java 4KB
.classpath 280B
.myhibernatedata 483B
lib
a.jar 661KB
共 17 条
- 1
heroxtl
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0