Using SFTP with Java
Learn about connecting to an SFTP server and Couchdrop with Java.
How to use SFTP with Java
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class SFTPExample { public static void main(String[] args) { String host = "sftp.example.com"; String username = "your_username"; String password = "your_password"; int port = 22; try { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.connect(); // Now you can perform SFTP operations using sftpChannel // Close the SFTP channel and session when done sftpChannel.disconnect(); session.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }sftpChannel.put("/path/to/local/file.txt", "/path/to/remote/file.txt");sftpChannel.get("/path/to/remote/file.txt", "/path/to/local/file.txt");String files = sftpChannel.ls("/path/to/remote/directory");
sftpChannel.disconnect(); session.disconnect();
Last updated
Was this helpful?