简体   繁体   中英

Progress bar for file transfer from Unix to Java

I know this question sounds repeated but the difference is that i am trying to implement progress bar for Unix-Java file transfer.I am using below code for downloading the file from Unix to my local host.

    String SFTPHOST = "xxx.xx.xx.xxx";
    int    SFTPPORT = 22; 
    String SFTPUSER = "abc"; 
    String SFTPPASS = "pwd"; 
    String SFTPWORKINGDIR = "/home/Shashank";  
    String SFTPDEST="G:\\xyz\\update.txt";
    Session     session     = null; 
    Channel     channel     = null; 
    ChannelSftp channelSftp = null;   
    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(); 
    channel = session.openChannel("sftp"); 
    channel.connect(); 
    channelSftp = (ChannelSftp)channel; 

    channelSftp.cd(SFTPWORKINGDIR); 

    byte[] buffer = new byte[1024]; 
    BufferedInputStream bis = new BufferedInputStream(channelSftp.get("update.txt")); 
    File newFile = new File(SFTPDEST); 
    OutputStream os = new FileOutputStream(newFile); 
    BufferedOutputStream bos = new BufferedOutputStream(os); 
    int readCount; 
    //System.out.println("Getting: " + theLine); 
    while( (readCount = bis.read(buffer)) > 0) { 
        System.out.println("Writing: " ); 
        bos.write(buffer, 0, readCount); 
    }
    bis.close(); 
    bos.close(); 
    }catch(Exception ex){ 
        ex.printStackTrace(); 
    }  

Please suggest me how to use progress bar in this code.I tried JSch sftp upload/download progress link but i think this isn't Java-unix file transfer.

Essentially you need to implement the SftpProgressMonitor interface in a class and then pass that as a parameter to your ChannelSftp put() or get() call.. (eg in your case

BufferedInputStream bis = new BufferedInputStream(channelSftp.get("update.txt"));

becomes

BufferedInputStream bis = new BufferedInputStream(channelSftp.get("update.txt", implementsSftpProgressMonitor));

In my case I just implemented the SftpProgressMonitor methods in the class which handled the SFTP transfers and passed this into the method, whatever works for you.

This interface has 3 methods, which the put and get methods will call during the course of the file being transferred to allow you to implement a progress bar.

init() - will be called when the transfer begins and passes a number of useful parameters including the total file size.

count() - gets called periodically during the file transfer, and passes the current amount of data which has been transferred since it was last called. Your implementing class should keep track of the totat transferred and then use this to calculate the total progress. You can also pass a return value in your implementation of this method to allow the transfer to be cancelled.

end() - gets called once the transfer is completed or is cancelled.

Hopefully using this you can work out how to display the progress for your application.

Edit: http://www.jcraft.com/jsch/examples/Sftp.java.html provides and example implementation of this interface, scroll down a bit until you get to the internal MyProgressMonitor class.

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