简体   繁体   中英

Using JSch Java library SftpProgressMonitor or any other class/method, How can I monitor the transfer speed of SFTP file transfer?

I am using the JSch Java library and wanted to track and print the speed of a SFTP file transfer at various different stages. Eg I want to know the speed of a file transfer at 20%,40%,60% and so on till it reached 100%. Currently the SftpProgressMonitor tells us the status of the file transfer ie the percentage of file that is transferred. I want to know the throughput of that too.

My code is:

 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.");
 }

Just calculate the speed yourself.

Remember, when a file transfer started (start_time) in the SftpProgressMonitor.init . You know how many bytes were transferred from the SftpProgressMonitor.count call (the count argument).

That's all you need to calculate the speed in bytes per second.

bps = count / (current_time - start_time)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM