使用JSch Java库SftpProgressMonitor或任何其他类/方法,如何监控SFTP文件传输的传输速度?

时间:2016-03-15 20:25:15

标签: java sftp jsch

我正在使用JSch Java库,并希望在各个不同阶段跟踪和打印SFTP文件传输的速度。例如。我想知道文件传输的速度在20%,40%,60%等等,直到达到100%。目前SftpProgressMonitor告诉我们文件传输的状态,即传输的文件的百分比。我也想知道它的吞吐量。

我的代码是:

 String SFTPHOST = "xx.xx.xx.xxx";
 int SFTPPORT = 22;
 String SFTPUSER = "xyz";
 String SFTPPASS = "password";
 String SFTPWORKINGDIR = "/home/abc";

 Session session = null;
 Channel channel = null;
 ChannelSftp channelSftp = null;
 System.out.println("preparing the host information for sftp.");
 try {
     JSch jsch = new JSch();
     session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
     session.setPassword(SFTPPASS);
     java.util.Properties config = new java.util.Properties();
     config.put("StrictHostKeyChecking", "no");
     session.setConfig(config);
     session.connect();
     System.out.println("Host connected.");
     channel = session.openChannel("sftp");
     channel.connect();
     System.out.println("sftp channel opened and connected.");
     channelSftp = (ChannelSftp) channel;
     channelSftp.cd(SFTPWORKINGDIR);
     File f = new File("C:\\Users\\Test.mp4");
     channelSftp.put(new FileInputStream(f), f.getName());
 } catch (Exception ex) {
      System.out.println("Exception found while tranfer the response.");
 }
 finally{
     channelSftp.exit();
     System.out.println("sftp Channel exited.");
     channel.disconnect();
     System.out.println("Channel disconnected.");
     session.disconnect();
     System.out.println("Host Session disconnected.");
 }

1 个答案:

答案 0 :(得分:1)

自己计算速度。

请记住,SftpProgressMonitor.init中的文件传输开始时(start_time)。您知道从SftpProgressMonitor.count调用(count参数)传输了多少字节。

您只需要计算每秒字节数的速度。

bps = count / (current_time - start_time)