0

I'm using this code to download a file, and I was wondering if its possible to pause the download and then resume it later, and if so how?:

    URL url = new URL(URL);
        URLConnection conexion = url.openConnection();

        conexion.connect();
        int lenghtOfFile = conexion.getContentLength();
        lenghtOfFile /= 100;

        InputStream input = new BufferedInputStream(url.openStream());
        OutputStream output = new FileOutputStream(Path + FileName);

        byte data[] = new byte[1024];
        long total = 0;

        int count = 0;
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
            total += count;
        }

        output.flush();
        output.close();
        input.close();
Omar
  • 7,835
  • 14
  • 62
  • 108

2 Answers2

4

This is only possible if the server supports HTTP range headers (introduced in HTTP 1.1)

See this question for examples in Java: how to use the HTTP range header in J2ME?.

"Pausing" could just mean reading some of the stream and writing it to disk. When resuming you would have to use the headers to specify what is left to download.

Community
  • 1
  • 1
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
-1

I think the support for the resume is at the server side, the clients cannot direct it.

Nrj
  • 6,723
  • 7
  • 46
  • 58