Service 的 RandomAccessFile 对象一直空 打断点了也看不出来 命名new了啊 而且文件长度获取一直是0
//初始化子线程
class InitThread extends Thread {
private FileInfo mFileInfo;
public InitThread(FileInfo mFileInfo) {
this.mFileInfo = mFileInfo;
}
public void run() {
HttpURLConnection connection = null;
RandomAccessFile raf = null;
try {
//连接网络文件
URL url = new URL(mFileInfo.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
int length = -1;
if (connection.getResponseCode() == 206) {
//获得文件长度
length = connection.getContentLength();
}
if (length <= 0) {
return;
}
File dir = new File(DOENLOAD_PATH);
if (!dir.exists()){
dir.mkdir();
}
//在本地创建文件
File file = new File(dir , mFileInfo.getFileName());
raf = new RandomAccessFile(file,"rwd");
//设置文件长度
raf.setLength(length);
mFileInfo.setLength(length);
handler.obtainMessage(MSG_INIT,mFileInfo).sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
connection.disconnect();
raf.close();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}