我正在尝试用java制作一个multiThread下载程序。它可以工作,但我下载的文件总是缺少一些字节。
我搜索并找到了许多multiThreaded网络爬虫的例子,但是并不简单,所以有人能告诉我我的方法是否有效吗?
我不知道问题是否在于字节的排序。
我试过BlockingQueue,但没有用
URL url = new URL(mURL);
final HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.connect();
final BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
final File f = new File("tr.exe");
if (f.exists()) {
f.delete();
}
// open the output file and seek to the start location
Thread t1 = new Thread() {
public void run() {
try {
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.seek(0);
int numRead;
int mStartByte = 0;
byte data[] = new byte[conn.getContentLength() / 2];
while (((numRead = in.read(data, 0, 1024)) != -1) && (mStartByte < conn.getContentLength() / 2)) {
// write to buffer
raf.write(data, 0, numRead);
// increase the startByte for resume later
mStartByte += numRead;
// increase the downloaded size
}
raf.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(MyDownloader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyDownloader.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
Thread t2 = new Thread() {
public void run() {
try {
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.seek(conn.getContentLengthLong() / 2);
int numRead;
int mStartByte = conn.getContentLength() / 2;
byte data[] = new byte[conn.getContentLength() / 2];
while (((numRead = in.read(data, 0, 1024)) != -1) && (mStartByte < conn.getContentLength())) {
// write to buffer
raf.write(data, 0, numRead);
// increase the startByte for resume later
mStartByte += numRead;
// increase the downloaded size
}
raf.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(MyDownloader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyDownloader.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
t1.start();
t2.start();发布于 2014-09-24 10:08:11
这是我注意到的第一个问题..。
byte[] data应该与您正在读取的块大小相同,您已经硬编码为1024。int mStartByte = conn.getContentLength() / 2,如果长度是一个奇数呢?发布于 2014-09-24 09:57:50
在我看来,您正在同时写入同一个文件,是的,顺序改变了,您甚至可能会丢失一些字节,现在您应该做的是编写文件的第一个线程的输出,例如'Thread1Output‘和第二个线程到'Thread2Output’,并在最后将两个文件组装到一个文件中,希望它对您有用,祝您好运,对不起,我提供了代码,因为我从来没有创建过一个下载程序=D。
https://stackoverflow.com/questions/26013563
复制相似问题