9

I wrote a program that downloads some files from some servers.
Currently program works properly.
But I want to add resume support to it.
I'm doing it like this But the result file is corrupted:

....

File fcheck=new File(SaveDir+"/"+filename);
if(resumebox.isSelected() && fcheck.exists()){
    connection.setRequestProperty("Range", "Bytes="+(fcheck.length())+"-");
}

connection.setDoInput(true);
connection.setDoOutput(true);

BufferedInputStream in = new BufferedInputStream (connection.getInputStream()); 

pbar.setIndeterminate(false);
pbar.setStringPainted(true);

java.io.FileOutputStream fos ;
if(resumebox.isSelected()){
    if(fcheck.exists()){
        if(connection.getHeaderField("Accept-Ranges").equals("bytes")){
            fos = new java.io.FileOutputStream(SaveDir+"/"+filename,true);
        }else{
            fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
        }
    }else{
        fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
    }
}else{
    fos = new java.io.FileOutputStream(SaveDir+"/"+filename);
}

....

I'm Testing it on a server that I know supports resume.
I downloaded some bytes.(72720)
Then Tried to resume it.
Then I opened file with a Hex editor , At offset 72720 the first Bytes are repeated:
Bytes 0-36: FLV.............«..........onMetaData
Bytes 72720-72756: FLV.............«..........onMetaData
It Starts download from the begining!
While when I do it by wget it does correctly and responses by Content-Range field!
Server responses with "302 FOUND" and a "206 Partial Content" in wget log.
Can "302 FOUND" cause the problem?

What is the problem ?
Thanks.

Ariyan
  • 14,760
  • 31
  • 112
  • 175
  • The other problem is that you've an unnecessarily deep nested if/else block. Make use of the `&&` operator. I.e. `if (a && b && c) { fos = new FOS(name, true); } else { fos = new FOS(name); }`. That's it. DRY. – BalusC Aug 05 '10 at 12:12
  • I summarized code! in those if else blocks I'm rising some messages so it should be as it is. but thank you for your response. – Ariyan Aug 05 '10 at 14:04
  • The 302 shouldn't be a problem since HTTPUrlConnection follows redirects by default. Unless you're turning redirects off explicitly, which I doubt since you're actually getting file contents – NG. Aug 05 '10 at 14:29

2 Answers2

16

Try:

connection.setRequestProperty("Range", "bytes=" + fcheck.length() + "-");

Lowercase the range specifier per the spec. Also, if your partial file was 500 bytes, that means your byte range that you have is 0-499, and you want 500+.

frogatto
  • 28,539
  • 11
  • 83
  • 129
NG.
  • 22,560
  • 5
  • 55
  • 61
  • Thanks. You are right. But It has another problem , I edited original post and added that problem.please check that out. thanks – Ariyan Aug 05 '10 at 14:06
  • You should consider using something like wireshark to make sure your request header is set properly. You can try using addRequestProperty instead of setRequestProperty, though I would hope they both do the same thing. – NG. Aug 05 '10 at 14:32
  • Hi, I captured using wireshark. it is set: "Range: bytes=257177-"! but it starts from byte 0 again! – Ariyan Aug 05 '10 at 16:06
  • Interesting - try comparing the headers you have when issuing the wget and your code. It looks right. – NG. Aug 05 '10 at 19:15
3

The problem is in (fcheck.length() - 1): this should be fcheck.length().

frogatto
  • 28,539
  • 11
  • 83
  • 129
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97