Following configuration can be used to change logged user directory. In normally, FTP directory is set to logging user home. Sometimes, you have to access directories outside of the logging SFTP user home.
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
This code snip can be used, when you are using vfs2 SFTP client.
Now, you can FTP files in any location using following SFTP URI.
"sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName
Change remote directory as you preferred.
Showing posts with label SFTP. Show all posts
Showing posts with label SFTP. Show all posts
Thursday, December 4, 2014
Tuesday, October 28, 2014
SFTP in JAVA
Following JAVA functions can be used to execute SFTP(Secure FTP).
public boolean putSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) throws FileSystemException {
boolean put = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
String filepath = localDirectory + fileName;
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");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName;
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
put = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return put;
}
public boolean getSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) {
boolean get = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
String filepath = localDirectory + fileName;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
get = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return get;
}
public boolean deleteSFTFile(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
boolean delete = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
if (remoteFile.exists()) {
if (remoteFile.delete()) {
delete = true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return delete;
}
public Collection getSFTPFileList(String sftpHost, String userID, String password, String remoteDirectory) throws FileSystemException {
Collection fileCollection = new ArrayList();
StandardFileSystemManager manager = new StandardFileSystemManager();
FileObject localFileObject = manager.resolveFile("sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory);
FileObject[] children = localFileObject.getChildren();
for (int i = 0; i < children.length; i++) {
fileCollection.add(children[ i].getName().getBaseName());
}
return fileCollection;
}
public boolean isRemoteFileExist(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
StandardFileSystemManager manager = new StandardFileSystemManager();
boolean isExist = false;
try {
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
isExist = remoteFile.exists();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
return isExist;
}
public boolean putSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) throws FileSystemException {
boolean put = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
String filepath = localDirectory + fileName;
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");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName;
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
put = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return put;
}
public boolean getSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) {
boolean get = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
String filepath = localDirectory + fileName;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
get = true;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return get;
}
public boolean deleteSFTFile(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
boolean delete = false;
StandardFileSystemManager manager = new StandardFileSystemManager();
try {
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
if (remoteFile.exists()) {
if (remoteFile.delete()) {
delete = true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
manager.close();
}
return delete;
}
public Collection getSFTPFileList(String sftpHost, String userID, String password, String remoteDirectory) throws FileSystemException {
Collection fileCollection = new ArrayList();
StandardFileSystemManager manager = new StandardFileSystemManager();
FileObject localFileObject = manager.resolveFile("sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory);
FileObject[] children = localFileObject.getChildren();
for (int i = 0; i < children.length; i++) {
fileCollection.add(children[ i].getName().getBaseName());
}
return fileCollection;
}
public boolean isRemoteFileExist(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
StandardFileSystemManager manager = new StandardFileSystemManager();
boolean isExist = false;
try {
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
+ remoteDirectory + fileName;
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
isExist = remoteFile.exists();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
manager.close();
}
return isExist;
}
Subscribe to:
Posts (Atom)