title: JAVA HttpURLConnection I/O Not working
body: <p>Hello My Respected Seniors <strong>:)</strong></p>
  
  <p><strong>My Goal:</strong> Download a URL Resource, given a URL, by using Multi-Threading in Java, i.e. download a single file into multiple pieces (much like how IDM does) &amp; at the end of download, combine all of them to 1 final file.</p>
  
  <p><em>Technology Using:</em> Java, RandomAccessFile, MultiThreading, InputStreams</p>
  
  <p><strong>Problem:</strong> 
  The file is downloaded fine with exact KB size, I've checked many times, but the final file is corrupted. For example, If I download an Image, it will be somewhat blurry, If I download an .exe, it downloads fine but when I run the .exe file, it says "media is damaged, retry download".</p>
  
  <p>This is my <strong>Main code</strong> from which I call to thread class with parameters such as fileName, starting Range and ending Range for a connection as well as a JProgressBar for every thread which will update its own respectively.</p>
  
  <pre><code>public void InitiateDownload()
  {
      HttpURLConnection uc = (HttpURLConnection) url.openConnection();
      uc.connect();
      long fileSize = uc.getContentLengthLong();
      System.out.println("File Size = "+ fileSize );
      uc.disconnect();
  
  
      chunkSize = (long) Math.ceil(fileSize/6);
  
      startFrom = 0;
      endRange = (startFrom + chunkSize) - 1;
  
      Thread t1 = new MyThread(url, fileName, startFrom, endRange, progressBar_1);
      t1.start();
      //-----------------------------------------
  
      startFrom += chunkSize;
      endRange = endRange + chunkSize;
      System.out.println("Part 2 :: Start = " + startFrom + "\tEnd To = " + endRange );
  
      Thread t2 = new MyThread(url, fileName, startFrom, endRange, progressBar_2);
      t2.start();
      //-----------------------------------------
      //..
      //..
      //..
      //-----------------------------------------
      startFrom += chunkSize;
      long temp = endRange + chunkSize;
      endRange = temp + (fileSize - temp);    //add any remaining bits, that were rounded off in division
  
      Thread t6 = new MyThread(url, fileName, startFrom, endRange, progressBar_6);
      t6.start();
      //-----------------------------------------
  }
  </code></pre>
  
  <p>Here is <strong>run()</strong> function of <strong>MyThread</strong> class:</p>
  
  <pre><code>public void run() {
  
      Thread.currentThread().setPriority(MAX_PRIORITY);
  
      System.setProperty("http.proxyHost", "192.168.10.50");
      System.setProperty("http.proxyPort", "8080");   
  
      HttpURLConnection uc = null;
      try  {
  
          uc = (HttpURLConnection) url.openConnection();
  
          uc.setRequestProperty("Range", "bytes="+startFrom+"-"+range);
          uc.connect();
          fileSize = uc.getContentLengthLong();
  
          inStream = uc.getInputStream();
  
          int[] buffer = new int[ (int) totalDownloadSize ];
  
          file.seek(startFrom);   //adjusted start of file
  </code></pre>
  
  <p><em><strong>THIS IS WHERE I THINK THE PROBLEM IS</em></strong>,</p>
  
  <p>run() continued...</p>
  
  <pre><code>for(int i = 0 ; i &lt; totalDownloadSize; i++)
          {
              buffer[i] = inStream.read();
              file.write(buffer[i]);
  
              //Updating Progress bars
              totalDownloaded = totalDownloaded + 1;
              int downloaded = (int) (100 * ( totalDownloaded/ (float) totalDownloadSize)) ;
              progressbar.setValue(  downloaded );
  
          }
  
  
          System.err.println( Thread.currentThread().getName() + "'s download is Finished!");
          uc.disconnect();
      }
      catch(IOException e) {
          System.err.println("Exception in " + Thread.currentThread().getName() + "\t Exception = " + e );
  
      }
      finally {
          try {
              file.close();
              if(inStream!=null)
                  inStream.close();
  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>Now the file is downloaded with complete size, but as I said, a little part of it is corrupt.
  <strong>Now,</strong>
  If I replace the <code>for loop</code> with following <code>while</code> loop, the problem is completely solved. </p>
  
  <pre><code>int bytesRead = 0;
          byte[] buffer = new byte[ (int) totalDownloadSize ];
  
          file.seek(startFrom);   //adjusted start of file
  
          while( (bytesRead = inStream.read(buffer) ) != -1 ) {
  
              file.write(buffer, 0, bytesRead);
  
          }
  </code></pre>
  
  <p><strong>BUT I NEED <code>for</code> LOOP TO MEASURE HOW MUCH FILE EACH THREAD HAS DOWNLOADED &amp; I WANT TO UPGRADE RESPECTIVE JPROGRESSBARs of THREADS.</strong></p>
  
  <p>Kindly help me out with the for loop logic.</p>
  
  <p><strong>OR</strong></p>
  
  <p>If you can advise on how can I upgrade Jprogressbars within <code>while</code> loop. I can't seem to find a way to <strong>quantify</strong> how much file a thread has downloaded...</p>
  
  <p>I've spent alot of hours &amp; I'm extremely tired now...</p>