2

我有两个类文件upload.javatransferProgress.javaupload.java制作小程序 GUI 并将文件上传到远程 SSH 服务器。transferProgress.java类给出传输百分比。上传完成的百分比可以在控制台中看到,但我希望它可以在 TextArea 和 java 进度条上看到。所以我让transferProgress.java继承upload.java 并附加到TextArea

我的问题是,TextAreaJProgressBar在文件传输过程中没有得到更新,但只有在文件传输完成时才会更新。传输完成后,TextArea显示日志并且 JProgressBar 设置为 100%。我的代码在文件传输期间不会更新TextAreaJProgressBar 。

如果我使用 setText() 而不是 append 来更新 TextArea,我可以看到实时进度更新,但 ProgressBar 仍然不会实时更新。

我无法弄清楚问题出在哪里。我将非常感谢您的帮助。

上传.java

package biforce;
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*; 
import java.io.*; 


import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class upload extends Applet
{

  String filename;
  int port = 22;
  String user = "root";
  String password = "mypass";
  String host = "192.168.0.5";
  String knownHostsFile = "/home/bishwo/.ssh/known_hosts";
  String sourcePath = "";
  String destPath = "/media/dfs/gwas/";

  JTextField txtField = new JTextField(20);
  static TextArea txtArea;
  static JProgressBar progressBar;

    @Override
  public void init(){

  // text Field     
  txtField.setEditable(false);
  txtField.setText("");

  // text area
  txtArea = new TextArea(4,40);
  txtArea.setEditable(false);

  // JprogressBar
  progressBar = new JProgressBar(0, 100);
  progressBar.setValue(0);
  progressBar.setStringPainted(true);

  // Label
  JLabel fileLabel = new JLabel("File");

  // buttons
  JButton upload = new JButton( "Upload" );
  JButton browse = new JButton( "Browse" );

  // browse file to be uploaded
  browse.addActionListener( 
      new ActionListener()
      {
            @Override
        public void actionPerformed( ActionEvent ae )
        {
          JFileChooser fc = new JFileChooser();
          fc.setCurrentDirectory( new File( "/home/bishwo/Desktop/" ) );
          int returnVal = fc.showOpenDialog( upload.this ); 
          String filePath="";
          if ( returnVal == JFileChooser.APPROVE_OPTION )   
          {  
            File aFile = fc.getSelectedFile();  
            filePath = aFile.getAbsolutePath(); 
            filename = aFile.getName();
          }
          txtField.setText(filePath);
        }
      });
  // upload the browsed file
  upload.addActionListener( 
      new ActionListener()
      {
            @Override
        public void actionPerformed( ActionEvent ae )
        {

          if(txtField.getText().length()==0)
          {
              JOptionPane.showMessageDialog(upload.this,"Please select a file to upload.","Error", JOptionPane.ERROR_MESSAGE);
          }
          else
          {
            try
            {
                sourcePath=txtField.getText();
                JSch jsch = new JSch();
                jsch.setKnownHosts(knownHostsFile);
                Session session = jsch.getSession(user, host, port);
                session.setPassword(password);
                session.connect(); 

                ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
                sftpChannel.connect();

                txtArea.setText("Uploading..");
                transferProgress progress = new transferProgress();
                sftpChannel.put( sourcePath,destPath+filename, progress);      
                System.out.println("\nUpload Completed");  

                sftpChannel.exit();
                session.disconnect();
                JOptionPane.showMessageDialog(upload.this,"Upload completed successfully.","Info", JOptionPane.INFORMATION_MESSAGE);

            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(upload.this,e,"Error", JOptionPane.ERROR_MESSAGE);
            }
          }
        }
      });

  add(fileLabel);
  add(txtField,"center");
  add(browse);
  add(upload);
  add(progressBar);
  add(txtArea);
  }

}

transferProgress.java

import com.jcraft.jsch.*;

public class transferProgress extends upload implements SftpProgressMonitor
{
    public double count=0;
    private int percentage;
    public double totalSize;
    private int lastPercentage;
    @Override
    public void init(int op, String src, String dest, long max) 
        {
        this.totalSize=max;
        }

    @Override
    public boolean count(long count) 
        {
        this.count += count;
        this.percentage = (int) ((this.count / this.totalSize) * 100.0);
        if (this.lastPercentage <= this.percentage - 5) 
            {
            this.lastPercentage= this.percentage;
            // setValue() does not work
            biforce.upload.progressBar.setValue(20);
            // append() does not work
            biforce.upload.txtArea.append(Integer.toString(this.percentage));
            // displays percentage completion on console
            System.out.println("Upload Completion "+this.percentage+" %");

            }
        return true;
        }

    @Override
    public void end() 
        {
        System.out.println("Total Copied "+this.percentage+" %");
        }
}
4

3 回答 3

1

您需要在单独的线程中更新进度条。

我建议让你的类transferProgress(注意:类名应该以大写字母开头)实现Runnable 接口,然后使用ExecutorService用这个类启动一个新线程。

例如

public class TransferProgress extends upload implements SftpProgressMonitor, Runnable {
    @Override
    public void run() {
       //update your progressbar here, basically the same as your count method
    }
}

public class upload extends Applet {
    //some code...
    upload.addActionListener( 
      new ActionListener()
       {
             @Override
             public void actionPerformed( ActionEvent ae )
             {
                 //some code....
                 //start a new thread here which executes your run method in the TransferProgress class
                 ExecutorService executor = Executors.newSingleThreadExecutor();
                 executor.submit(new TransferProgress());
                 //some code...
             }
       }
    );

}
于 2011-11-07T09:06:34.487 回答
0

transferProgress 类中的 count() 方法提供 20 次进度百分比,直到文件被上传。

在我的情况下 SwingUtilities.invokeLater 没有成功。我使用了从上传类执行的 SwingWorker,它工作正常。(PS - 我在课堂上传'public'中制作了progressBar)

public class uploadWorker extends SwingWorker<Integer, Integer>
{
    @Override
        protected Integer doInBackground() throws Exception
        {
                //some codes .....

                try{sftpChannel.put(upload.sourcePath,upload.destPath, new transferProgress()); }
               catch(Exception e){System.out.println(e);}

                Thread.sleep(1000);
                return 42;
        }

        protected void done()
        {

                System.out.print("done");

        }
}
于 2011-11-24T10:33:03.853 回答
0

您永远不应该从事件分派线程 (EDT) 执行长时间运行的操作,因为在此期间 GUI 被阻塞。您在这里观察到了这种效果。EDT 是运行所有动作侦听器的线程,并且还重新绘制您的 GUI 对象。只要动作侦听器正在运行,就不会调度其他事件,也不会发生绘画 => 您将不会在进度条或文本区域上看到任何更新。(不过,一旦您的动作侦听器完成,它们就会更新。)

因此,使用单独的线程来上传文件,即elseActionListener 中 -block 内的所有内容,就像 flash 的答案一样。

然后更新 GUI 的所有内容都应在 EDT 中再次完成,因此请使用EventQueue.invokeLater(或SwingUtilities.invokeLater) 作为 GUI 更新部分:

EventQueue.invokeLater(new Runnable() { public void run() {
       biforce.upload.progressBar.setValue(20);
       biforce.upload.txtArea.append(Integer.toString(this.percentage));
}});
于 2011-11-07T12:32:40.253 回答