Created
January 30, 2019 09:24
-
-
Save rbaul/a2e1790c2ea241b98c3f9d632c6e540c to your computer and use it in GitHub Desktop.
SFTP monitoring with JSCH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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