[英]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. 我知道这个问题听起来很重复,但是不同之处在于我正在尝试实现Unix-Java文件传输的进度条。我正在使用下面的代码从Unix将文件下载到本地主机。
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. 请建议我如何在此代码中使用进度条。我尝试了JSch sftp上传/下载进度链接,但我认为这不是Java-unix文件传输。
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 本质上,您需要在一个类中实现SftpProgressMonitor接口,然后将其作为参数传递给ChannelSftp put()或get()调用。(例如,在您的情况下)
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. 就我而言,我只是在处理SFTP传输的类中实现了SftpProgressMonitor方法,并将其传递给该方法,不管对您有用。
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. 该接口有3个方法,在文件传输过程中将调用put和get方法,以使您能够实现进度条。
init() - will be called when the transfer begins and passes a number of useful parameters including the total file size. init()-在传输开始并传递许多有用的参数(包括文件总大小)时将被调用。
count() - gets called periodically during the file transfer, and passes the current amount of data which has been transferred since it was last called. count()-在文件传输期间定期调用,并传递自上次调用以来已传输的当前数据量。 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. end()-传输完成或取消后被调用。
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. 编辑: http : //www.jcraft.com/jsch/examples/Sftp.java.html提供了此接口的示例实现,向下滚动一点,直到获得内部MyProgressMonitor类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.