代码如下:
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异常,这个怎么解决啊?在线等!
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异常,这个怎么解决啊?在线等!
解决方案 »
- 3dmax材质导入到android opengl es
- 关于Android控件方面的提!
- 本人寻找软件工程视屏教程,希望大家传传,谢谢啦!!!
- 类似傲游 或者 IE7 的检索一个网页中是否含有RSS源,如果含有源工具栏的RSS源 图标就变成可用的,请问它们是怎么检索网页含有源的
- android的C开发和java开发区别?
- CSipSimple注册问题Unable to create/send REGISTER
- 求大神指点 Android 上拉下拉隐藏或显示部分控件
- 帮忙看看这个是为什么..
- 关于Android中WebView的问题
- 新手,写了一个欢迎界面然后再跳转到主界面,但是闪退了
- 电子书 PDF文档 观看学习效率低怎么办?
- apk和odex合并问题
麻烦贴出比较全面的错误日志信息.. 大家才好帮你分析