sftp file transfer

This commit is contained in:
DOHA 2019-07-18 00:23:38 +03:00
parent c1afe47b93
commit feb5b850ff
2 changed files with 129 additions and 1 deletions

View File

@ -171,6 +171,24 @@
<artifactId>jmimemagic</artifactId> <artifactId>jmimemagic</artifactId>
<version>${jmime-magic.version}</version> <version>${jmime-magic.version}</version>
</dependency> </dependency>
<!-- 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> </dependencies>
<build> <build>
@ -249,7 +267,7 @@
<!-- util --> <!-- util -->
<commons-lang3.version>3.5</commons-lang3.version> <commons-lang3.version>3.5</commons-lang3.version>
<bouncycastle.version>1.55</bouncycastle.version> <bouncycastle.version>1.60</bouncycastle.version>
<commons-codec.version>1.10</commons-codec.version> <commons-codec.version>1.10</commons-codec.version>
<commons-math3.version>3.6.1</commons-math3.version> <commons-math3.version>3.6.1</commons-math3.version>
<decimal4j.version>1.0.3</decimal4j.version> <decimal4j.version>1.0.3</decimal4j.version>
@ -262,6 +280,10 @@
<streamex.version>0.6.5</streamex.version> <streamex.version>0.6.5</streamex.version>
<vavr.version>0.9.0</vavr.version> <vavr.version>0.9.0</vavr.version>
<opencsv.version>4.1</opencsv.version> <opencsv.version>4.1</opencsv.version>
<!-- sftp -->
<jsch.version>0.1.55</jsch.version>
<sshj.version>0.27.0</sshj.version>
<vfs.version>2.3</vfs.version>
<!-- testing --> <!-- testing -->
<assertj.version>3.6.1</assertj.version> <assertj.version>3.6.1</assertj.version>
<avaitility.version>1.7.0</avaitility.version> <avaitility.version>1.7.0</avaitility.version>

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;
}
}