0

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.

4
  • Looks like you're trying to run winscp and get upload progress from it. I don't think this will work the way you expect it to. You'd better try to implement uploading/downloading your self using java library like JSch. Commented Feb 19, 2014 at 4:37
  • @IvanNevostruev : changed the my ques.Could you help now.plz Commented Feb 19, 2014 at 7:41
  • I think you're almost there. stackoverflow.com/questions/7977844/… has exactly that you need. Basically you'll need to implement SystemOutProgressMonitor differently. Based on how you want to display progress to user. Commented Feb 19, 2014 at 7:50
  • @IvanNevostruev: m completely blank right now.couldn't find much about SystemOutProgressMonitor on google. Commented Feb 19, 2014 at 8:05

1 Answer 1

1

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.. (e.g 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.