我有两个类fils upload.java 和 transferProgress.java 。 upload.java 制作applet GUI并将文件上传到远程SSH服务器。 transferProgress.java 类给出了传输百分比。完成上传百分比可以在控制台中看到,但我想在TextArea和java进度条上看到它。所以我让 transferProgress.java 继承 upload.java 并追加到 TextArea 。
我的问题是, TextArea 和 JProgressBar 在文件传输过程中没有得到更新,但只有在文件传输完成后才会更新。传输完成后, TextArea 显示日志,JProgressBar设置为100%。我的代码在文件传输过程中不会更新 TextArea 和 JProgressBar 。
如果我使用setText()而不是append来更新TextArea,我可以看到实时进度更新,但仍然没有实时更新ProgressBar。
我无法弄清楚问题出在哪里。我将非常感谢你的帮助。
upload.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+" %");
}
}
答案 0 :(得分:1)
您需要在单独的主题中更新进度条。
我建议让你的班级transferProgress
(注意:班级名称应以大写字母开头)实施Runnable Interface,然后使用ExecutorService开始一个新的帖子类。
E.g。
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...
}
}
);
}
答案 1 :(得分:0)
您永远不应该从事件派发线程(EDT)执行长时间运行的操作,因为GUI在此期间被阻止。你在这里观察到了这种效果。 EDT是运行所有动作侦听器的线程,并且还重新绘制GUI对象。只要动作侦听器正在运行,就不会调度任何其他事件,也不会发生绘画=&gt;您将看不到进度条或文本区域的更新。 (但是,一旦你的动作听众完成,它们就会更新。)
因此,使用一个单独的线程来上传文件,即ActionListener中else
- 块内的所有内容,就像在flash的答案中一样。
然后,应该再次在EDT中完成更新GUI的所有内容,因此请使用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));
}});
答案 2 :(得分:0)
count()方法在文件上传之前提供了20次进度百分比。
在我的情况下,SwingUtilities.invokeLater没有成功。我使用SwingWorker,从上传类执行,它工作。 (P.S - 我在班级上传'public'中取得进度吧)
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");
}
}