Skip to content

Instantly share code, notes, and snippets.

@rbaul
Created January 30, 2019 09:24
Show Gist options
  • Save rbaul/a2e1790c2ea241b98c3f9d632c6e540c to your computer and use it in GitHub Desktop.
Save rbaul/a2e1790c2ea241b98c3f9d632c6e540c to your computer and use it in GitHub Desktop.
SFTP monitoring with JSCH
import com.jcraft.jsch.SftpProgressMonitor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@RequiredArgsConstructor
public class CustomSftpProgressMonitor implements SftpProgressMonitor {
private final long totalbytes;
private long uploadedBytes = 0;
private double previousPercentage = 0;
@Getter @Setter
private ProgressListener progressListener;
@Override
public void init(int op, String src, String dest, long max) {
System.out.println("Start uploading...");
}
@Override
public boolean count(long bytes) {
uploadedBytes +=bytes;
double currentPercentage = calculatePercentage(uploadedBytes, totalbytes);
if(currentPercentage != previousPercentage){
previousPercentage = currentPercentage;
if(progressListener != null){
progressListener.change(currentPercentage);
}
}
return true;
}
@Override
public void end() {
System.out.println("Success uploaded...");
}
private double calculatePercentage(double obtained, double total) {
return Math.round(obtained * 100 / total);
}
}
public static void main(String[] args) throws IOException, JSchException {
Session session = connect("some_ip", 22, "username", "password");
if(session.isConnected()){
// SFTP
ChannelSftp channelSftp = null;
try {
File file = new File("someFolder/someFile");
if(file.exists()){
// SFTP
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
// transfer to specific directory
try{
channelSftp.mkdir("temp");
} catch (SftpException e){
//If cannot make directory, means directory already created
}
channelSftp.cd("temp");
// Monitoring
CustomSftpProgressMonitor monitor = new CustomSftpProgressMonitor(file.length());
monitor.setProgressListener(currentPercentage -> System.out.println("Percentage: " + currentPercentage + " %"));
// Send
channelSftp.put(new FileInputStream(file), file.getName(), monitor);
}
} catch (SftpException e) {
e.printStackTrace();
} finally {
if(channelSftp != null){
channelSftp.exit();
}
session.disconnect();
}
}
}
public static Session connect(String host, Integer port, String user, String password) throws JSchException {
Session session = null;
try {
JSch jsch = new JSch();
if(port != null){
session = jsch.getSession(user, host, port.intValue());
}else{
session = jsch.getSession(user, host);
}
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
//time out
session.connect(3000);
} catch (JSchException e) {
e.printStackTrace();
System.out.println("SFTPUitl connection error");
throw e;
}
return session;
}
public interface ProgressListener {
void change(double currentPercentage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment