Help! Downloading a file from remote Server

I am currently doing a program for downloading a specified file from the remote server i.e from any website. If the downloading process stops in between due to some problem in the internet connection, then my program needs to start the downloading the file again to the local directory from where it was left.

My idea is that, getting the size of the remote file initially when the downloading process starts and if the connection is interrupted, i will check the size of the downloaded file in the local directory with the remote file size. If the remote file size is greater than the localfile size, i need to start downloading the file where it was left i.e start downloading the next bit from the remote file.

Is there any way to get the contents of the remote file from the specified point say 50th byte from a total file size of 100 kb using java?

which concept to useFtpClient or sockets?

iam a little bit confused!!!!

Please help me.

Thanks in advance.

[1011 byte] By [Revaa] at [2007-11-26 21:23:37]
# 1
Check the Javadoc for URL, HttpURLConnection, and the Range HTTP header. That's all you need (seriously) (For an HTTP download of course :-p)
benubacha at 2007-7-10 3:03:10 > top of Java-index,Java Essentials,Java Programming...
# 2

For HTTP you can use this function to open the stream with offset:

InputStream openStream(URL url, long offset) {

try {

URLConnection uc = url.openConnection();

uc.setRequestProperty("Authorization", "Basic " + (new BASE64Encoder()).encode("username:password".getBytes()));

if (offset != 0 && url.openConnection().getHeaderField(0).toUpperCase().startsWith("HTTP/1.1"))

uc.setRequestProperty("Range", "bytes=" + offset + "-");

return new BufferedInputStream(uc.getInputStream());

} catch (MalformedURLException mfURLe) {

System.err.println("Malformed URL in Client Updater. Unable to receive update list: " + mfURLe.getMessage());

} catch (IOException ioe) {

System.err.println("Unable to open input stream: " + ioe.getMessage());

}

return null;

}

Rodney_McKaya at 2007-7-10 3:03:10 > top of Java-index,Java Essentials,Java Programming...