Merge pull request #7346 from Doha2012/master

sftp file transfer
This commit is contained in:
Loredana Crusoveanu 2019-07-19 10:16:18 +03:00 committed by GitHub
commit 4eaf15ed51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 152 additions and 0 deletions

4
libraries-io/README.md Normal file
View File

@ -0,0 +1,4 @@
### Relevant Articles:

40
libraries-io/pom.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>libraries-io</artifactId>
<name>libraries-io</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- sftp file transfer -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>${jsch.version}</version>
</dependency>
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>${sshj.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>${vfs.version}</version>
</dependency>
</dependencies>
<properties>
<!-- sftp -->
<jsch.version>0.1.55</jsch.version>
<sshj.version>0.27.0</sshj.version>
<vfs.version>2.4</vfs.version>
</properties>
</project>

View File

@ -0,0 +1 @@
This is a sample text content

View File

@ -0,0 +1,106 @@
package org.baeldung.java.io.remote;
import java.io.IOException;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.VFS;
import org.junit.Test;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.sftp.SFTPClient;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
public class SftpFileTransferLiveTest {
private String remoteHost = "HOST_NAME_HERE";
private String username = "USERNAME_HERE";
private String password = "PASSWORD_HERE";
private String localFile = "src/main/resources/input.txt";
private String remoteFile = "welcome.txt";
private String localDir = "src/main/resources/";
private String remoteDir = "remote_sftp_test/";
private String knownHostsFileLoc = "/Users/USERNAME/known_hosts_sample";
@Test
public void whenUploadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
ChannelSftp channelSftp = setupJsch();
channelSftp.connect();
channelSftp.put(localFile, remoteDir + "jschFile.txt");
channelSftp.exit();
}
@Test
public void whenDownloadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
ChannelSftp channelSftp = setupJsch();
channelSftp.connect();
channelSftp.get(remoteFile, localDir + "jschFile.txt");
channelSftp.exit();
}
@Test
public void whenUploadFileUsingSshj_thenSuccess() throws IOException {
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.put(localFile, remoteDir + "sshjFile.txt");
sftpClient.close();
sshClient.disconnect();
}
@Test
public void whenDownloadFileUsingSshj_thenSuccess() throws IOException {
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.get(remoteFile, localDir + "sshjFile.txt");
sftpClient.close();
sshClient.disconnect();
}
@Test
public void whenUploadFileUsingApacheVfs_thenSuccess() throws IOException {
FileSystemManager manager = VFS.getManager();
FileObject local = manager.resolveFile(System.getProperty("user.dir") + "/" + localFile);
FileObject remote = manager.resolveFile("sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteDir + "vfsFile.txt");
remote.copyFrom(local, Selectors.SELECT_SELF);
local.close();
remote.close();
}
@Test
public void whenDownloadFileUsingApacheVfs_thenSuccess() throws IOException {
FileSystemManager manager = VFS.getManager();
FileObject local = manager.resolveFile(System.getProperty("user.dir") + "/" + localDir + "vfsFile.txt");
FileObject remote = manager.resolveFile("sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteFile);
local.copyFrom(remote, Selectors.SELECT_SELF);
local.close();
remote.close();
}
// ====== ssh-keyscan -H -t rsa remoteHost >> known_hosts_sample
private ChannelSftp setupJsch() throws JSchException {
JSch jsch = new JSch();
jsch.setKnownHosts(knownHostsFileLoc);
Session jschSession = jsch.getSession(username, remoteHost);
jschSession.setPassword(password);
//jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.connect();
return (ChannelSftp) jschSession.openChannel("sftp");
}
private SSHClient setupSshj() throws IOException {
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
client.connect(remoteHost);
client.authPassword(username, password);
return client;
}
}

View File

@ -506,6 +506,7 @@
<module>libraries-security</module>
<module>libraries-server</module>
<module>libraries-http</module>
<module>libraries-io</module>
<module>linkrest</module>
<module>logging-modules</module>
<module>lombok</module>