Search code examples
javainputstreamskip

Download Manager Network Failure Issue


I am making a download utility in java which will download files from the ftp server. I am using URLConnection class to establish connection and inputstream as the stream object. The problem that i am facing is in the network failure scenario i.e while downloading a particular file if the connectivity is lost and after the connectivity is restored i want it to resume downloading from where it had stopped previously.

To achieve this i have done the foll :-

/*Here,I am trying to skip the number of bytes downloaded so far so as to get the pointer in stream from where it should resume downloading.The skip() method may not necessarily skip the number of bytes given as a parameter and hence the while loop. */

while(totalByteSkipped!=downloaded) 
{ 
      bytesSkipped = stream.skip(downloaded-totalByteSkipped); 

      totalByteSkipped = totalByteSkipped+bytesSkipped; 
} 

But this approach of manually skipping so many bytes is very slow and affects the overall time of download, hence in such a case i need a mechanism to get the pointer in the stream at the point where it had stopped downloading.

Hoping to get an alternative approach on the same. Thanks!!


Solution

  • You need to implement FPT restart in order to resume downloads. If your server supports it, then you can tell it the offset in the file.

    Is there any reason why you are not using an already existent FTP Client? Commons Net FTP Client supports restart.