将 JProgressBar 与下载进程连接

发布于 2025-01-08 04:27:54 字数 1800 浏览 0 评论 0原文

我有下面的代码。我无法让它发挥作用。

我必须提到 URL 正在重定向。我的意思是 url = http://www.thissite.com 并重定向到 http://www.othersite.com。但我想让它与初始 url 一起工作。

 public void download(String address, String localFileName, JProgressBar progress ) {
    OutputStream out = null;
    URLConnection conn = null;
    InputStream in = null;

    try {

          URL url = new URL(address);



        // Open an output stream to the destination file on our local filesystem
        out = new BufferedOutputStream(new FileOutputStream("/MusicDownloads/"+localFileName));
        conn = url.openConnection();

        in = conn.getInputStream();

        int length = conn.getContentLength(); //find out how long the file is, any good webserver should provide this info
         int current = 0;
          progress.setMaximum(length); //we're going to get this many bytes
          progress.setValue(0); //we've gotten 0 bytes so far

        // Get the data
        byte[] buffer = new byte[1024];
        int numRead = 0;

        while ((numRead = in.read(buffer)) != -1) {
            current=0;
            out.write(buffer, current, numRead);

              current += numRead; //we've progressed a little so update current

            progress.setValue(current); //tell progress how far we are


        }
        // Done! Just clean up and get out
    } catch (Exception exception) {
        exception.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException ioe) {
            // Shouldn't happen, maybe add some logging here if you are not
            // fooling around ;)
        }
    }
}

I have the below code. I can't make it work.

I have to mention that the URL is redirecting. I mean that url = http://www.thissite.com and redirects to http://www.othersite.com. But I want to get it work with the initial url.

 public void download(String address, String localFileName, JProgressBar progress ) {
    OutputStream out = null;
    URLConnection conn = null;
    InputStream in = null;

    try {

          URL url = new URL(address);



        // Open an output stream to the destination file on our local filesystem
        out = new BufferedOutputStream(new FileOutputStream("/MusicDownloads/"+localFileName));
        conn = url.openConnection();

        in = conn.getInputStream();

        int length = conn.getContentLength(); //find out how long the file is, any good webserver should provide this info
         int current = 0;
          progress.setMaximum(length); //we're going to get this many bytes
          progress.setValue(0); //we've gotten 0 bytes so far

        // Get the data
        byte[] buffer = new byte[1024];
        int numRead = 0;

        while ((numRead = in.read(buffer)) != -1) {
            current=0;
            out.write(buffer, current, numRead);

              current += numRead; //we've progressed a little so update current

            progress.setValue(current); //tell progress how far we are


        }
        // Done! Just clean up and get out
    } catch (Exception exception) {
        exception.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException ioe) {
            // Shouldn't happen, maybe add some logging here if you are not
            // fooling around ;)
        }
    }
}

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

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

发布评论

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

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文