使用 apache http 组件监控下载进度

发布于 2024-08-22 12:51:39 字数 205 浏览 4 评论 0原文

我有一些大文件需要使用 Apache 的 Http 组件 下载,我希望能够显示一些进度信息我的网络应用程序的管理控制台中的用户。使用此库监视正在进行的下载的正确方法是什么?我将创建一个单例来管理正在进行的下载,我需要一种方法来检查打开的连接并找出它们在任何给定时间的位置。

I have some big files to download with Apache's Http Components and I want to be able to display some progress info to users in the admin console of my web app. What is the right way to monitor a download in progress using this library? I will create a singleton to manage the ongoing downloads, what I need is a way to inspect the open connections and find out where they are at any given time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-08-29 12:51:39
  1. 首先获取 Content-Length 响应头
  2. 以流的形式获取结果并逐字节处理(批量)
  3. 计算已处理的字节数并向用户显示已处理字节数占总字节数的百分比字节(在第一步中获得)
  1. Obtain the Content-Length response header first
  2. Get the result as stream and process it byte-by-byte (in batches)
  3. Count the bytes processed and show the user the percentage of processed bytes from the total number of bytes (obtained on the 1st step)
仅冇旳回忆 2024-08-29 12:51:39

添加到 @Bozho 的答案,这里是如何下载文件、计算下载进度百分比以及更新 JProgressBar 的示例代码

// Download update
    public static void update(final int totalBytes, final JProgressBar progressBar) {
        // Download file using buffer
        try (BufferedInputStream in = new BufferedInputStream(new URL(API_DOMAIN + "download").openStream());
             FileOutputStream fileOutputStream = new FileOutputStream(Files.dataDir + File.separator + "client.jar")) {

            byte[] dataBuffer = new byte[1024];
            int totalBytesRead = 0, bytesRead;
            int progress = 0;

            // Process buffer
            while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                fileOutputStream.write(dataBuffer, 0, bytesRead);

                // Calculate progress
                totalBytesRead += bytesRead;
                progress = (int) Math.floor(((double) totalBytesRead / totalBytes) * 100D);
                log.info(progress + "%");

                // Update UI
                int finalProgress = progress;
                SwingUtilities.invokeLater(() -> {
                    progressBar.setValue(finalProgress);
                    progressBar.setString(finalProgress + "%");
                });
            }
        } catch (IOException e) {
            // handle exception
            log.error("@Services~ Failed to update client due to: " + e.getMessage(), e);
        }
    }

Adding to @Bozho's answer, here is example code how to download a file, calculate download progress as a percentage, and update a JProgressBar

// Download update
    public static void update(final int totalBytes, final JProgressBar progressBar) {
        // Download file using buffer
        try (BufferedInputStream in = new BufferedInputStream(new URL(API_DOMAIN + "download").openStream());
             FileOutputStream fileOutputStream = new FileOutputStream(Files.dataDir + File.separator + "client.jar")) {

            byte[] dataBuffer = new byte[1024];
            int totalBytesRead = 0, bytesRead;
            int progress = 0;

            // Process buffer
            while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                fileOutputStream.write(dataBuffer, 0, bytesRead);

                // Calculate progress
                totalBytesRead += bytesRead;
                progress = (int) Math.floor(((double) totalBytesRead / totalBytes) * 100D);
                log.info(progress + "%");

                // Update UI
                int finalProgress = progress;
                SwingUtilities.invokeLater(() -> {
                    progressBar.setValue(finalProgress);
                    progressBar.setString(finalProgress + "%");
                });
            }
        } catch (IOException e) {
            // handle exception
            log.error("@Services~ Failed to update client due to: " + e.getMessage(), e);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文