I want to transfer files to a remote location and I am bound to use a proxy server for that. I was able to connect to the FTP location via following command :
sftp -o "ProxyCommand /usr/bin/nc -X connect -x <proxy_host>:<proxy_port> %h %p" username@ftp_server:
But I want to automate this process of file transfer and I am using JSch for SFTP and snippet of code is below:
String sourceFile = sourceDir + fileName;
JSch jsch = new JSch();
int port = Integer.parseInt(getFtpPort());
Session session = jsch.getSession(getUserName(), getHost(), port);
session.setConfig(STRICT_HOST_CHECKING, ANSWER);
session.setProxy(new ProxyHTTP(<proxy_host>, <proxy_port>));
session.setPassword(getPassword());
session.connect();
Channel channel = session.openChannel(FILE_PROTOCOL);
channel.connect();
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(desDir);
File fileToTransfer = new File(sourceFile);
sftpChannel.put(new FileInputStream(fileToTransfer), fileName);
With above code I am getting following exception:
Caused by: com.jcraft.jsch.JSchException: ProxyHTTP: java.io.IOException
at com.jcraft.jsch.ProxyHTTP.connect(ProxyHTTP.java:158)
at com.jcraft.jsch.Session.connect(Session.java:210)
at com.jcraft.jsch.Session.connect(Session.java:162)
No, it's not risky to give JSch your private key. In order to make asymmetric cryptography work, you have to use a private key. In this case, JSch is doing the job for you, but it won't send it to anyone, it's just using it to decrypt data you receive, and encrypt data you send.
2.1 In JSch, we can use put and get to do file transfer between servers. We use put to transfer files from a local system to the remote server. We use get to download files from a remote server to the local system. 2.2 Password authentication.
JSch also called “Java Secure Shell” is a Java implementation of SSH2. It allows you to connect to the Java application via an SSH server then transfer files. In addition, you can use the JSch library to copy files to remote machines without manual intervention.
From Spring Tool Suite IDE select menu File -> New -> Spring Starter Project. On the New Spring Starter Project popup input new project spring-boot-sftp information as following screenshot. In order to transfer files via SFTP we will use JSch (or Java Secure Channel) library which is a pure Java implementation of SSH2.
SSHJ also allows us to use Password or Public Key Authentication to access the remote server. We'll use the Password Authentication in our example: 3.3. Uploading a File With SSHJ Similar to JSch, we'll use the SFTPClient.put () method to upload a file to the remote server: We have two new variables here to define:
This worked for me.
JSch jsch = new JSch();
java.util.Properties config = new java.util.Properties();
Session session = jsch.getSession(RemoteUserName, RemoteIpAddr, RemotePortNo);
session.setPassword(RemotePassword);
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setProxy(new ProxyHTTP(ProxyName, ProxyPort));
session.connect();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With