title: Java: resume Download in URLConnection
body: <p>I wrote a program that downloads some files from some servers.<br>
  Currently program works properly.<br>
  But I want to add resume support to it.<br>
  I'm doing it like this But the result file is corrupted:  </p>
  
  <pre><code>....
  
  File fcheck=new File(SaveDir+"/"+filename);
  if(resumebox.isSelected() &amp;&amp; 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);
  }
  
  ....
  </code></pre>
  
  <p>I'm Testing it on a server that I know supports resume.<br>
  I downloaded some bytes.(72720)<br>
  Then Tried to resume it.<br>
  Then I opened file with a Hex editor , At offset 72720 the first Bytes are repeated:<br>
  Bytes 0-36: FLV.............«..........onMetaData<br>
  Bytes 72720-72756: FLV.............«..........onMetaData<br>
  It Starts download from the begining!<br>
  While when I do it by wget it does correctly and responses by Content-Range field!<br>
  Server responses with "302 FOUND" and a "206 Partial Content" in wget log.<br>
  Can "302 FOUND" cause the problem?  </p>
  
  <p>What is the problem ?<br>
  Thanks.</p>