Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transfer a file using a proxy with JSch library

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)
like image 601
Amaresh Avatar asked Jan 29 '14 07:01

Amaresh


People also ask

Is JSch secure?

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.

How to do file transfer between servers in jsch?

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.

What is the use of jsch?

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.

How to use Spring Boot SFTP with jsch?

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.

How to use sshj to upload a file to a remote server?

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:


1 Answers

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();
like image 165
Manish Avatar answered Sep 21 '22 11:09

Manish