在 Java 小程序中显示 FTP 文件上传期间的进度

时间:2022-11-07
本文介绍了在 Java 小程序中显示 FTP 文件上传期间的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

好的,我让上传者使用 Java FTP 上传文件,我想更新标签和进度条.带有百分比文本的标签,带有百分比 int 值的条形图.现在使用当前代码只能在上传结束时获得 100 条和完整条.在上传期间,它们都没有改变.

OK so I have the uploader uploading files using the Java FTP, I would like to update the label and the progress bar. Label with the percent text, bar with the percent int value. Right now with the current code only get the 100 and full bar at the end of the upload. During the upload none of them change.

这里是:

    OutputStream output = new BufferedOutputStream(ftpOut);
    CopyStreamListener listener = new CopyStreamListener() {
        public void bytesTransferred(long totalBytesTransferred, int bytesTransferred, long streamSize) {
            System.out.printf("
%-30S: %d / %d", "Sent", totalBytesTransferred, streamSize);
            ftpup.this.upd(totalBytesTransferred,streamSize);
        }
        public void bytesTransferred(CopyStreamEvent arg0) { }
    };

    Util.copyStream(input, output, ftp.getBufferSize(), f.length(), listener);      
}

public void upd(long num, long size){
    int k = (int) ((num*100)/size);
    System.out.println(String.valueOf(k));
    this.d.setText(String.valueOf(k));
    //d.setText(String.valueOf(k));
    progressBar.setValue(k);
}

推荐答案

从它的声音(并且缺乏任何证据证明)听起来你在 事件调度线程

From the sounds of it (and lacking any evidence to the contree) it sounds like your processing a time consuming action in the Event Dispatching Thread

您可能想阅读 Concurrency in Swing 了解一些进一步了解

You might like to read Concurrency in Swing for some further insight

我建议使用 SwingWorker 来执行实际转移利用其内置的进度支持

I'd suggest using a SwingWorker to perform the actual transfer & take advantage of its built in progress support

看到源代码后更新

  1. 请勿将重量较重的组件与重量较轻的组件混用.将Applet改为JApplet,将TextField改为JTextField,不要使用Canvas使用 JPanelJComponent
  2. 如果您希望其他人阅读您的代码,请为您的变量使用正确的名称,我不知道 p 是什么.
  3. 你的 Thread 没用.而不是启动线程并使用它的 run 方法,您只需在它的构造函数中进行下载调用.这对您没有任何帮助...
  1. Don't mix heavy weight components with light weight components. Change Applet to JApplet, change TextField to JTextField, don't use Canvas use a JPanel or JComponent
  2. If you expect other people to read your code, please use proper names for your variables, I have no idea what p is.
  3. Your Thread is useless. Rather then starting the thread and using it's run method you simply make your download call within it's constructor. This will do nothing for you...

删除 MyThread 的实现并将其替换为

Remove your implementation of MyThread and replace it with

public class MyWorker extends SwingWorker<Object, Object> {

    private URL host;
    private File outputFile;

    public MyWorker(URL host, File f) {
        this.host = host;
        outputFile = f;
    }

    @Override
    protected Object doInBackground() throws Exception {

        // You're ignoring the host you past in to the constructor
        String hostName = "localhost";
        String username = "un";
        String password = "pass";
        String location = f.toString();

        //FTPClient ftp = null;

        ftp.connect(hostName, 2121);
        ftp.login(username, password);

        ftp.setFileType(FTP.BINARY_FILE_TYPE);

        ftp.setKeepAlive(true);
        ftp.setControlKeepAliveTimeout(3000);
        ftp.setDataTimeout(3000); // 100 minutes
        ftp.setConnectTimeout(3000); // 100 minutes

        ftp.changeWorkingDirectory("/SSL");

        int reply = ftp.getReplyCode();
        System.out.println("Received Reply from FTP Connection:" + reply);

        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Connected Success");
        }
        System.out.println(f.getName().toString());

        File f1 = new File(location);
        in = new FileInputStream(f1);

        FileInputStream input = new FileInputStream(f1);
        // ftp.storeFile(f.getName().toString(),in);

        //ProgressMonitorInputStream is= new ProgressMonitorInputStream(getParent(), "st", in);
        OutputStream ftpOut = ftp.storeFileStream(f.getName().toString());


        System.out.println(ftpOut.toString());
        //newname hereSystem.out.println(ftp.remoteRetrieve(f.toString()));
        OutputStream output = new BufferedOutputStream(ftpOut);
        CopyStreamListener listener = new CopyStreamListener() {
            public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {

                setProgress((int) Math.round(((double) totalBytesTransferred / (double) streamSize) * 100d));

            }

            @Override
            public void bytesTransferred(CopyStreamEvent arg0) {
                // TODO Auto-generated method stub
            }
        };

        Util.copyStream(input, output, ftp.getBufferSize(), f.length(), listener);

        return null;

    }
}

o (??) 的 ActionListener 中,将线程执行代码替换为

In your ActionListener of o (??) replace the thread execution code with

try {
    MyWorker worker = new MyWorker(new URL("http://localhost"), file);
    worker.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("progress")) {
                Integer progress = (Integer) evt.getNewValue();
                progressBar.setValue(progress);
            }
        }
    });
    worker.execute();
} catch (MalformedURLException ex) {
    ex.printStackTrace();
}

注意.您忽略了传递给构造函数的 URL.http://不是 ftp://所以我怀疑这会起作用......

Note. You are ignoring the URL you pass to the constructor. http:// is not ftp:// so I doubt this will work...

这篇关于在 Java 小程序中显示 FTP 文件上传期间的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录? 下一条:没有了

相关文章

最新文章