Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何继续下载文件并在java中显示进度_Java_File_Download_Resume - Fatal编程技术网

如何继续下载文件并在java中显示进度

如何继续下载文件并在java中显示进度,java,file,download,resume,Java,File,Download,Resume,我想用Java编写一种方法来恢复下载文件,并尽可能显示进度 以下代码用于减去下载文件totalSize-downloaded的总大小,而不是完成下载 URL url = new URL(urlFile); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); File SDCardRoot = Environment.getExternalStorag

我想用Java编写一种方法来恢复下载文件,并尽可能显示进度

以下代码用于减去下载文件totalSize-downloaded的总大小,而不是完成下载

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();

这可能对暂停和恢复有用,但请指定确切的问题

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();
   }

您至少需要告诉我们您正在使用什么来下载文件,以及您希望如何显示进度。您可能需要向我们展示您迄今为止所写的内容。目前,你的问题太广泛,任何人都无法给你一个有用的答案。代码是一个问题,但我不知道它在哪里