初始化线程里 length=conn.getContentLength();返回值一直是-1
class InitThread extends Thread {
private FileInfo mFileInfo = null;
public InitThread(FileInfo mFileInfo) {
this.mFileInfo = mFileInfo;
}
public void run() {
HttpURLConnection conn = null;
RandomAccessFile raf = null;
try {
URL url = new URL(mFileInfo.getUrl());
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
int length = -1;
if(conn.getResponseCode() == HttpStatus.SC_OK)
{
length = conn.getContentLength(); //获得的值一直是-1,无法下载
}
if(length <=0)
{
return;
}
File dir = new File(DOWNLOAD_PATH);
if(!dir.exists())
{
dir.mkdir();
}
File file = new File(dir,mFileInfo.getFileName());
raf = new RandomAccessFile(file, "rwd");
raf.setLength(length);
mFileInfo.setLength(length);
mHandler.obtainMessage(MSG_INIT,mFileInfo).sendToTarget();
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try {
raf.close();
conn.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}