如何使用JProgressBar显示文件复制进度?

发布于 2024-07-12 01:55:45 字数 71 浏览 13 评论 0原文

我正在开发一个通过网络传输文件的项目,我想合并一个 JProgressBar 来显示文件传输过程中的进度,但我需要这方面的帮助。

I am working on a project that transfers files over a network and I want to incorporate a JProgressBar to show the progress during file transfer but I need help on that.

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

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

发布评论

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

评论(4

凌乱心跳 2024-07-19 01:55:45

您可能会发现 ProgressMonitorInputStream 最简单,但如果这对您来说还不够,请查看其源代码以准确获取您想要的内容。

 InputStream in = new BufferedInputStream(
                     new ProgressMonitorInputStream(
                              parentComponent,
                              "Reading " + fileName,
                              new FileInputStream(fileName)
                     )
                  );

要使用不同的传输方法,请用适当的流替换 FileInputStream。

You probably would find a ProgressMonitorInputStream easiest, but if that doesn't do enough for you, look at its source code to get exactly what you want.

 InputStream in = new BufferedInputStream(
                     new ProgressMonitorInputStream(
                              parentComponent,
                              "Reading " + fileName,
                              new FileInputStream(fileName)
                     )
                  );

To use a different transfer method then substitute an appropriate stream for FileInputStream.

赏烟花じ飞满天 2024-07-19 01:55:45
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JProgressBar;

....

public OutputStream loadFile(URL remoteFile, JProgressBar progress) throws IOException
{
    URLConnection connection = remoteFile.openConnection(); //connect to remote file
    InputStream inputStream = connection.getInputStream(); //get stream to read file

    int length = connection.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

    ByteArrayOutputStream out = new ByteArrayOutputStream(); //create our output steam to build the file here

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

    while((bytesRead = inputStream.read(buffer)) != -1) //keep filling the buffer until we get to the end of the file 
    {   
        out.write(buffer, current, bytesRead); //write the buffer to the file offset = current, length = bytesRead
        current += bytesRead; //we've progressed a little so update current
        progress.setValue(current); //tell progress how far we are
    }
    inputStream.close(); //close our stream

    return out;
}

我很确定这会起作用。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JProgressBar;

....

public OutputStream loadFile(URL remoteFile, JProgressBar progress) throws IOException
{
    URLConnection connection = remoteFile.openConnection(); //connect to remote file
    InputStream inputStream = connection.getInputStream(); //get stream to read file

    int length = connection.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

    ByteArrayOutputStream out = new ByteArrayOutputStream(); //create our output steam to build the file here

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

    while((bytesRead = inputStream.read(buffer)) != -1) //keep filling the buffer until we get to the end of the file 
    {   
        out.write(buffer, current, bytesRead); //write the buffer to the file offset = current, length = bytesRead
        current += bytesRead; //we've progressed a little so update current
        progress.setValue(current); //tell progress how far we are
    }
    inputStream.close(); //close our stream

    return out;
}

I'm pretty sure this will work.

╰ゝ天使的微笑 2024-07-19 01:55:45

听起来您应该使用 SwingWorker,如此核心 Java 技术提示中所述。 另请参见使用 Swing 工作线程

It sounds like you should be using a SwingWorker, as described in this Core Java Tech Tip. See also Using a Swing Worker Thread.

绝不放开 2024-07-19 01:55:45

好吧这里是JProgressBar的教程,也许是会有帮助的。

Well here is a tutorial for JProgressBar, maybe that will help.

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