How to sftp in java using jsch : Example
Hi,
Well i got some interesting work today. Transfer a file from a machine to another using java. From last couple of months i was working on uploading the excel files using UI, which is quite easy and for me just some hours of work. But this time i need to upload a file on server and after finish of upload, need to transfer it to another machine which will consume it. Quite interesting stuff cause was exhausted by doing the same stuff, excel file upload and UI from last 2 or 3 mnths.
Well to do this i Google some key words and found using Java Secure Channel jsch library we can do it easily in JAVA. It is also used by ANT, Eclipse, NetBeans and some other big projects. Then i thought to give it a try and wrote a Util class for my project using jsch. I added copy, and move methods in class for copy the file (without deleting the original location) and to Move the file (After copying delete the original file) respectively. You need to download and add jsch.jar file in ur classpath.
Below is the code i wrote for my sftp util class, use it share it and enjoy the coding.
package com.maddy.util;
import java.io.File;
import java.io.FileInputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SftpUtility {
private String sftpHost ;
private int sftpPort ;
private String sftpUser ;
private String sftpPassword ;
private String sftpDir ;
public SftpUtility(String sftpHost, int sftpPort, String sftpUser,
String sftpPassword, String sftpDir) {
super();
this.sftpHost = sftpHost;
this.sftpPort = sftpPort;
this.sftpUser = sftpUser;
this.sftpPassword = sftpPassword;
this.sftpDir = sftpDir;
}
public String getSftpHost() {
return sftpHost;
}
public void setSftpHost(String sftpHost) {
this.sftpHost = sftpHost;
}
public int getSftpPort() {
return sftpPort;
}
public void setSftpPort(int sftpPort) {
this.sftpPort = sftpPort;
}
public String getSftpUser() {
return sftpUser;
}
public void setSftpUser(String sftpUser) {
this.sftpUser = sftpUser;
}
public String getSftpPassword() {
return sftpPassword;
}
public void setSftpPassword(String sftpPassword) {
this.sftpPassword = sftpPassword;
}
public String getSftpDir() {
return sftpDir;
}
public void setSftpDir(String sftpDir) {
this.sftpDir = sftpDir;
}
public boolean moveFileToDir(String localFilePath){
return moveFileToDir(localFilePath, null, null, true);
}
public boolean moveFileToDir(String localFilePath, String remoteDirPath){
return moveFileToDir(localFilePath, remoteDirPath, null, true);
}
public boolean moveFileToDir(String localFilePath, String remoteDirPath, String remoteFileName){
return moveFileToDir(localFilePath, remoteDirPath, remoteFileName, true);
}
public boolean copyFileToDir(String localFilePath){
return moveFileToDir(localFilePath, null, null, false);
}
public boolean copyFileToDir(String localFilePath, String remoteDirPath){
return moveFileToDir(localFilePath, remoteDirPath, null, false);
}
public boolean copyFileToDir(String localFilePath, String remoteDirPath, String remoteFileName){
return moveFileToDir(localFilePath, remoteDirPath, remoteFileName, false);
}
public boolean moveFileToDir(String localFilePath, String remoteDirPath, String remoteFileName, boolean isDelete){
boolean returnResult = false;
boolean deleteSuccess = false;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try{
JSch jsch = new JSch();
session = jsch.getSession(this.sftpUser,this.sftpHost,this.sftpPort);
session.setPassword(this.sftpPassword);
java.util.Properties config = new java.util.Properties();
config.put(“StrictHostKeyChecking”, “no”);
session.setConfig(config);
session.connect();
channel = session.openChannel(“sftp”);
channel.connect();
channelSftp = (ChannelSftp)channel;
if(null != remoteDirPath)
channelSftp.cd(remoteDirPath);
else
channelSftp.cd(this.sftpDir);
File f = new File(localFilePath);
String fileName = f.getName();
if(null != remoteFileName && remoteFileName.length() > 0)
fileName = remoteFileName;
channelSftp.put(new FileInputStream(f), fileName);
//Disconnecting the channel
channel.disconnect();
//Disconnecting the session
session.disconnect();
if(isDelete){
deleteSuccess = f.delete();
}else{
deleteSuccess = true;
}
returnResult = deleteSuccess;
}catch(Exception ex){
ex.printStackTrace();
}
return returnResult;
}
}
Cheers !!
Open Mind, Open Source.



