代码如下:
public void run(){
try {
int startPosition = threadIndex * block;
int endPosition = (threadIndex + 1) * block - 1;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes=" + startPosition + "-" + endPosition);
conn.setConnectTimeout(5000);
conn.connect();
RandomAccessFile raf = new RandomAccessFile(saveFile, "rwd");
raf.seek(startPosition);
InputStream inStream = conn.getInputStream();
byte [] buffer = new byte[1024];
int len = -1;
while((len = inStream.read(buffer) ) != -1){
downloadLength = downloadLength + len;
raf.write(buffer, 0, len);
// 使用监听器实时监听下载的总长度
if(listener != null){
listener.onDownloadSize(downloadLength);
}
}
raf.close();
inStream.close();
System.out.println("线程id:" + threadIndex + " 下载完成");
if(downloadLength == fileLength){
System.out.println("文件下载完成");
downloadLength = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
不知道为什么在2.2上可以用,到了4.1.2上就抛出java.io.EOFException异常,这个怎么解决啊?在线等!