日期:2014-05-20  浏览次数:20837 次

程序错误
以下是关于线程代码的内容和运行后提示如下异常:

Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 0
at download.DownLoadThread.run(DownLoadThread.java:60)
自己是在无法解决,情大侠们帮忙解决下;


package download;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.JOptionPane;

public class DownLoadThread extends Thread{



long DownLength = 0; //已下载文件大小

public void run() {
String url = DownloadNewFrm.getTxtAddress().getText(); //获取url地址
String currentFileThreadName = DownloadNewFrm.txtSave.getText() + DownloadNewFrm.txtFile.getText(); //完整路径名
InputStream in = null;
FileOutputStream out = null;

try {

URL Url = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) (Url.openConnection());

in = httpConnection.getInputStream();
out = new FileOutputStream(currentFileThreadName);

int i = 0;
long nFileLength = DownloadNewFrm.getFileSize(); // 文件大小

while ((i = in.read()) != -1) {
out.write(i);// 写文件
DownLength ++;
long num = DownLength * 100L/nFileLength;
MultiDownloaderFrm.pgbRate.setValue((int)num);
//MultiDownloaderFrm.getTxaSpeedSize().setText("" + DownLength/1024 + "KB");
}



int threadNum = Integer.parseInt(MultiDownloaderFrm.txaSwing.getText());
long block = nFileLength/threadNum;
DownLoadThread[] downThreads = new DownLoadThread[0];
if(nFileLength%threadNum == 0) {
for(int j = 0; j < threadNum; j++) {
downThreads[j].DownLength=j*block;
downThreads[j].DownLength=block;
}
} else {
for(int j = 0; j < threadNum; j++) {
downThreads[j].DownLength=j*block;
downThreads[j].DownLength=block;
}
downThreads[0].DownLength = block - nFileLength%threadNum;
}
}catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
in.close();
out.close();

} catch (IOException e1) {
e1.printStackTrace();
}

}
}

}








------解决方案--------------------