我正在使用 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.");
}
最佳答案
自己计算一下速度。
请记住,当 SftpProgressMonitor.init
中开始文件传输 (start_time
) 时。您知道从 SftpProgressMonitor.count
传输了多少字节调用(count
参数)。
这就是计算每秒字节数所需的全部内容。
bps = count / (current_time - start_time)
关于java - 使用 JSch Java 库 SftpProgressMonitor 或任何其他类/方法,如何监控 SFTP 文件传输的传输速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36021433/