Here is a sample code to SFTP all files from the local folder to a remote folder
Our code should create the folder in the remote box if the folder doesn't exist already
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import javax.faces.context.FacesContext;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.ADFContext;
import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;
import oracle.jbo.Row;
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;
private void transfer() {
StandardFileSystemManager manager = new StandardFileSystemManager();
System.out.println("this.getSelectedCompany()" + this.getSelectedCompany());
ADFContext adfCtx = ADFContext.getCurrent();
Map pageFlowScope = adfCtx.getPageFlowScope();
try {
String serverAddress = "**host_ip_address**";
String userId = "**userID**";
String password = "**password**";
String remoteDirectory =
"/a//b//c//d//e//f//outbound//xyz//vijay//" +
pageFlowScope.get("company").toString() + "//";
String localDirectory =
"//i//j//k//l//m//n//" +
pageFlowScope.get("company").toString() + "//";
File dir = new File(localDirectory);
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
//Initializes the file manager
manager.init();
for (File file : directoryListing) {
System.out.println("1--------------" + file.getPath());
if (!file.exists())
file.createNewFile();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
//Create the SFTP URI using the host name, userid, password, remote path and file name
String sftpUri =
"sftp://" + userId + ":" + password + "@" + serverAddress + "/" + remoteDirectory +
file.getName();
// manager.resolveFile(sftpUri);
System.out.println("sftpUri---->" + sftpUri);
// Create local file object
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println("localFile---->" + localFile);
// Create remote file object
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("File upload successful");
}
} else {
// Handle the case where dir is not really a directory.
// Checking dir.isDirectory() above would not be sufficient
// to avoid race conditions with another process that deletes
// directories.
throw new RuntimeException("Error. Directory file not found");
}
} catch (Exception ex) {
ex.printStackTrace();
// return false;
} finally {
manager.close();
}
}
Our code should create the folder in the remote box if the folder doesn't exist already
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import javax.faces.context.FacesContext;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.ADFContext;
import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;
import oracle.jbo.Row;
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;
private void transfer() {
StandardFileSystemManager manager = new StandardFileSystemManager();
System.out.println("this.getSelectedCompany()" + this.getSelectedCompany());
ADFContext adfCtx = ADFContext.getCurrent();
Map pageFlowScope = adfCtx.getPageFlowScope();
try {
String serverAddress = "**host_ip_address**";
String userId = "**userID**";
String password = "**password**";
String remoteDirectory =
"/a//b//c//d//e//f//outbound//xyz//vijay//" +
pageFlowScope.get("company").toString() + "//";
String localDirectory =
"//i//j//k//l//m//n//" +
pageFlowScope.get("company").toString() + "//";
File dir = new File(localDirectory);
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
//Initializes the file manager
manager.init();
for (File file : directoryListing) {
System.out.println("1--------------" + file.getPath());
if (!file.exists())
file.createNewFile();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
//Create the SFTP URI using the host name, userid, password, remote path and file name
String sftpUri =
"sftp://" + userId + ":" + password + "@" + serverAddress + "/" + remoteDirectory +
file.getName();
// manager.resolveFile(sftpUri);
System.out.println("sftpUri---->" + sftpUri);
// Create local file object
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println("localFile---->" + localFile);
// Create remote file object
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("File upload successful");
}
} else {
// Handle the case where dir is not really a directory.
// Checking dir.isDirectory() above would not be sufficient
// to avoid race conditions with another process that deletes
// directories.
throw new RuntimeException("Error. Directory file not found");
}
} catch (Exception ex) {
ex.printStackTrace();
// return false;
} finally {
manager.close();
}
}