这是我的代码
private static void DownloadFile() throws IOException {
BufferedInputStream in = null;
File file=new File("temporary.file");
RandomAccessFile raf=null;
long fSize=0;
if(file.exists()){
raf=new RandomAccessFile(file, "rw");
fSize=raf.length();
System.out.println("File Exists size is "+fSize );
}
else{//2210413
raf=new RandomAccessFile(file, "rw");
}
URL mURL=new URL("http://down5.game.uc.cn/s/5/5
/20130517175858a128ee_1750544ZFbuE.xcxxsdjb_1365659155330.apk");
HttpURLConnection conn = (HttpURLConnection)mURL.openConnection();
String byteRange = fSize + "-";
conn.setRequestProperty("Range", byteRange);
conn.setConnectTimeout(60000);
conn.connect();
in = new BufferedInputStream(conn.getInputStream());
in.skip(fSize);
int mEndByte=conn.getContentLength();
System.out.println("Total size:: "+mEndByte );
raf.seek(fSize);
byte data[] = new byte[1024];
int numRead;
while(((numRead = in.read(data,0,1024)) != -1)){
raf.write(data,0,numRead);
fSize += numRead;
}
if (raf != null) {
try {
raf.close();
} catch (IOException e) {}
}if (in != null) {
try {
in.close();
} catch (IOException e) {}
}
);
}}
此方法工作正常,但需要很长时间才能恢复。
我的问题是,有没有有效的方法来做到这一点?在我的代码中,我认为in.skip();
需要时间。