以下是关于线程代码的内容和运行后提示如下异常: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();
}
}
}
}
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();
}
}
}
}
1、这个数组长度是0;应该是:threadNum
2、这个数组的元素没有初始化;应该是:downThreads[i] = new DownLoadThread()
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: -1
at download.DownLoadThread.run(DownLoadThread.java:54)
还是不对啊
downloadThreads[i] = new DownLoadThread();
for (int i=0;i<threadNum;i++) {
downloadThreads[i] = new DownLoadThread();
}
但你前面的循环已经用过了i,而且退出循环的值就是-1,所以就直接出错了。
怕了你了,这样吧:
DownLoadThread[] downThreads = new DownLoadThread[0];
修改为:
DownLoadThread[] downThreads = new DownLoadThread[threadNum];
for (int cnt=0;cnt<threadNum;i++) {
downloadThreads[cnt] = new DownLoadThread();
}
for (int cnt=0;cnt<threadNum;i++) {最后应该是 cnt++
for (int cnt=0; cnt<threadNum; cnt++) {
long timeStart = System.currentTimeMillis();
最后:
long timeSpend = System.currentTimeMillis() - timeStart;timeSpend 是所经过时间,单位:毫秒。
是你没有 System.out.println("经过时间:" + timeSpend + "ms"); // 就是显示出来
这里:
} catch (IOException e1) {
e1.printStackTrace();
}修改为:
} catch (IOException e1) {
e1.printStackTrace();
} finally {
long timeSpend = System.currentTimeMillis() - timeStart;
System.out.println("经过时间:" + timeSpend + "ms"); // 就是显示出来
}
out.write(i);// 写文件
DownLength ++;
long num = DownLength * 100L/nFileLength;
MultiDownloaderFrm.pgbRate.setValue((int)num);
//MultiDownloaderFrm.getTxaSpeedSize().setText("" + DownLength/1024 + "KB");
}如果加到这个循环里,就会运行,但是加到后面他还是不显示
是不是这个类还是有问题啊?
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 start = System.currentTimeMillis(); // 当前时间,用于记录开始统计时间
long end = start;
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[] downloadThreads = new DownLoadThread[threadNum];
for (int cnt=0;cnt<threadNum;cnt++) {
downloadThreads[cnt] = new DownLoadThread(); // 初始化线程
}
if(nFileLength%threadNum == 0) {
for(int j = 0; j < threadNum; j++) {
downloadThreads[j].DownLength=j*block;
downloadThreads[j].DownLength=block;
}
} else {
for(int j = 0; j < threadNum; j++) {
downloadThreads[j].DownLength=j*block;
downloadThreads[j].DownLength=block;
}
downloadThreads[0].DownLength = block - nFileLength%threadNum;
}
}catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
in.close();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
finally {
MultiDownloaderFrm.txaCondition.setText("下载结束");
System.out.println("下载结束");
// 获得当前时间,然后与start时间比较,如果不一样,利用当前完成的总数除以所使用的时间,获得一个平均下载速度
end = System.currentTimeMillis();
long timeSpend = end - start ;
MultiDownloaderFrm.txaTime.setText("" + timeSpend);// 就是显示出来
}
}
}
}
用 “read(byte[] buff),一次读取一组,可以设置 byte[] buff = new byte[4096];”写的话怎么调用啊?
while ((i = in.read(buff)) > 0) {
out.write(buff);// 写文件
DownLength += i;
...
}