Site icon NgDeveloper

SFTP in Java Sample Working Program

SFTP File Upload in Java:
FTP can be done with Apache Commons and commons-logging jar. If you want to do SFTP you need the below jars,

Required Jars:
[plain gutter=”false”]
1. commons-logging-1.1.1.jar
2. commons-vfs2-2.0 (or) commons-vfs-1.0 (try vfs2.0 jar first, if any unexpected exception then try with vfs1.0.jar)
3. jsch-0.1.50 (First check without this jar, if any unexpected exceptions then add this jar also and try)
[/plain]

SFTP File Upload Program:

[java]
import java.io.File;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

public class SFTPUpload {
public static void main(String[] args) {
SFTPUpload sendMyFiles = new SFTPUpload();
sendMyFiles.fileUpload();
}

public boolean fileUpload() {
System.out.println("SFTP Upload Process Begins");
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
String serverAddress = "SERVER_HOST_OR_IP_ADDRESS";
String userName = "USERNAME";
String password = "PASSWORD";
String dstnDirectory = "DESTINATION_PATH";
String srcDirectory = "SOURCE_PATH";
String fileToUpload = "javadomain.txt";

String filepath = srcDirectory + fileToUpload;
File file = new File(filepath);

if (!file.exists())
throw new RuntimeException("Error. Local file not found");

manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");

// Below line create the Destination path inside destination path
// and transfer the javadomain.txt file
// SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts,true);

// Below line transfer the javadomain.txt file to destination path

SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts,
false);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

String sftpUri = "sftp://" + userName + ":" + password + "@"
+ serverAddress + "/" + dstnDirectory + fileToUpload; System.out.println("SFTP URI: " + sftpUri);

FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println("Source Path: " + localFile);

FileObject remoteFile = manager.resolveFile(sftpUri, opts);
System.out.println("Destination Path: " + remoteFile);

remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("SFTP Upload Done");
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
manager.close();
}
return true;
}

}
[/java]

Destination Path Problem:

Eg:
Local_direcotry = /usr/Naveen/files
Remote_direcotry = /usr/javadomain/files

SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts,false);
False – It will take the given remote directory path as destination and transfer the files.
True – It will create the same path from the given destination path and then transfers the files.

Example:False – /usr/javadomain/files
True – /usr/javadomain/files/usr/javadomain/files and transfer/upload the files.

Recommended Books:

Exit mobile version