-2

I want to code a way to resume the download file in Java and show the progress if possible.

The following code was used to subtract the total size of the downloaded file (totalSize - downloaded) instead of completing the download.

URL url = new URL(urlFile);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            File SDCardRoot = Environment.getExternalStorageDirectory();
            file = new File(SDCardRoot,"/MySchool/"+Folder+"/"+nameBook.getText().toString()+".pdf");
            urlConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
            urlConnection.setDoOutput(true);
            urlConnection.connect();
            outputStream = new FileOutputStream(file);
            InputStream inputStream = urlConnection.getInputStream();
            //this is the total size of the file which we are downloading
            totalSize = urlConnection.getContentLength();

            byte[] buffer = new byte[1024];
            int bufferLength = 0;

            while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                outputStream.write(buffer, 0, bufferLength);
                downloadedSize += bufferLength;
                }
            outputStream.close();
  • You need to tell us (at least) what you are using to download files, and how you want to show progress. And you probably need to show us what you have written so far. Currently, your question is Too Broad for anyone to give you a useful answer. – Stephen C Apr 15 '17 at 04:45

1 Answers1

0

This might be useful for pause and resume but please specify exact problem.

if (outputFileCache.exists())
    {
        connection.setAllowUserInteraction(true);
        connection.setRequestProperty("Range", "bytes=" + outputFileCache.length() + "-");
    }

    connection.setConnectTimeout(14000);
    connection.setReadTimeout(20000);
    connection.connect();

    if (connection.getResponseCode() / 100 != 2)
        throw new Exception("Invalid response code!");
    else
    {
        String connectionField = connection.getHeaderField("content-range");

        if (connectionField != null)
        {
            String[] connectionRanges = connectionField.substring("bytes=".length()).split("-");
            downloadedSize = Long.valueOf(connectionRanges[0]);
        }

        if (connectionField == null && outputFileCache.exists())
            outputFileCache.delete();

        fileLength = connection.getContentLength() + downloadedSize;
        input = new BufferedInputStream(connection.getInputStream());
        output = new RandomAccessFile(outputFileCache, "rw");
        output.seek(downloadedSize);

        byte data[] = new byte[1024];
        int count = 0;
        int __progress = 0;

        while ((count = input.read(data, 0, 1024)) != -1 
                && __progress != 100) 
        {
            downloadedSize += count;
            output.write(data, 0, count);
            __progress = (int) ((downloadedSize * 100) / fileLength);
        }

        output.close();
        input.close();
   }
Jayveer Parmar
  • 500
  • 7
  • 25