下面的SFTP实用工具提供了上载,存在,下载,移动和删除操作的示例。我已经在几个项目中使用了它。以下代码仅使用来自Apache Commons
VFS库的api调用。
请确保将以下JAR放入项目的构建路径中:
- commons-logging-1.1.3.jar
- commons-vfs2-2.0.jar
- hamcrest-core-1.3.jar
- jsch-0.1.50.jar
任何其他兼容版本也可以。
import java.io.File;import org.apache.commons.vfs2.FileObject;import org.apache.commons.vfs2.FileSystemException;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 SFTPUtility { public static void main(String[] args) { String hostName = "PutYourHostNameHere"; String username = "PutYourUserNameForHostHere"; String password = "PutYourPasswordForHostHere"; String localFilePath = "C:\FakePath\FakeFile.txt"; String remoteFilePath = "/FakeRemotePath/FakeRemoteFile.txt"; String remoteTempFilePath = "/FakeRemoteTempPath/FakeRemoteTempFile.txt"; upload(hostName, username, password, localFilePath, remoteFilePath); exist(hostName, username, password, remoteFilePath); download(hostName, username, password, localFilePath,remoteFilePath); move(hostName, username, password, remoteFilePath, remoteTempFilePath); delete(hostName, username, password, remoteFilePath); } public static void upload(String hostName, String username, String password, String localFilePath, String remoteFilePath) { File file = new File(localFilePath); if (!file.exists()) throw new RuntimeException("Error. Local file not found"); StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Create local file object FileObject localFile = manager.resolveFile(file.getAbsolutePath()); // Create remote file object FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); // Copy local file to sftp server remoteFile.copyFrom(localFile, Selectors.SELECT_SELF); System.out.println("File upload success"); } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } } public static boolean move(String hostName, String username, String password, String remoteSrcFilePath, String remoteDestFilePath){ StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Create remote object FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteSrcFilePath), createDefaultOptions()); FileObject remoteDestFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteDestFilePath), createDefaultOptions()); if (remoteFile.exists()) { remoteFile.moveTo(remoteDestFile);; System.out.println("Move remote file success"); return true; } else{ System.out.println("Source file doesn't exist"); return false; } } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } } public static void download(String hostName, String username, String password, String localFilePath, String remoteFilePath) { StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Append _downlaod_from_sftp to the given file name. //String downloadFilePath = localFilePath.substring(0, localFilePath.lastIndexOf(".")) + "_downlaod_from_sftp" + localFilePath.substring(localFilePath.lastIndexOf("."), localFilePath.length()); // Create local file object. Change location if necessary for new downloadFilePath FileObject localFile = manager.resolveFile(localFilePath); // Create remote file object FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); // Copy local file to sftp server localFile.copyFrom(remoteFile, Selectors.SELECT_SELF); System.out.println("File download success"); } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } } public static void delete(String hostName, String username, String password, String remoteFilePath) { StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Create remote object FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); if (remoteFile.exists()) { remoteFile.delete(); System.out.println("Delete remote file success"); } } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } } // Check remote file is exist function: public static boolean exist(String hostName, String username, String password, String remoteFilePath) { StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Create remote object FileObject remoteFile = manager.resolveFile(createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); System.out.println("File exist: " + remoteFile.exists()); return remoteFile.exists(); } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } } public static String createConnectionString(String hostName, String username, String password, String remoteFilePath) { return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath; } public static FileSystemOptions createDefaultOptions() throws FileSystemException { // Create SFTP options FileSystemOptions opts = new FileSystemOptions(); // SSH Key checking SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); // Root directory set to user home SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false); // Timeout is count by Milliseconds SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); return opts; }}


