0

Here is my code

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) {}
     }

);
}}

This method is working properly, but taking long time to resume. My Question is Is there any efficient way to do this? In my code I think in.skip(); is taking time.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
MDEVLP
  • 124
  • 1
  • 3
  • 12
  • I'm surprised it's working at all - it looks to me like you're doubling the offset, by using both the `Range` header *and* then skipping bytes. I'd expect just using the `Range` header to be enough. – Jon Skeet Jul 28 '13 at 06:48
  • But it just appends to larger size if i dont use skip. – MDEVLP Jul 28 '13 at 06:50
  • Are you sure the server you're using supports Range at all? You should really check the Content-Range header first. I suggest you use an HTTP library (e.g. Apache HttpClient) to handle all of this for you - it's likely to be a lot simpler. – Jon Skeet Jul 28 '13 at 07:14
  • server suppports range. thanks for suggestion. – MDEVLP Jul 28 '13 at 07:36

0 Answers0