原文链接:http://pro.ctlok.com/2011/06/apache-commons-vfs-for-sftp.html
在 Java 使用 FTP 去上載檔案,下載檔案或去做其他操作也是很簡單,因為 Java API 已經包括了對 FTP 的支援。如果不是使用 FTP,而是使用 SFTP: SSH File Transfer Protocol,使用Java API 實作便有點麻煩了。
幸好 Apache Commons VFS 替我們實作了 SFTP 的操作,Apache Commons VFS 配合 JSch 使我們可以很簡單地使用 SSH 連接 Server 和上載檔案,下載檔案等等操作。
廢話不多說,首先要準備 3 個 Library:
- public static String createConnectionString(String hostName,
- String username, String password, String remoteFilePath) {
- // result: "sftp://user:123456@domainname.com/resume.pdf
- 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, true);
-
- // Timeout is count by Milliseconds
- SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
-
- return opts;
- }
- public static void upload(String hostName, String username,
- String password, String localFilePath, String remoteFilePath) {
-
- File f = new File(localFilePath);
- if (!f.exists())
- throw new RuntimeException("Error. Local file not found");
-
- StandardFileSystemManager manager = new StandardFileSystemManager();
-
- try {
- manager.init();
-
- // Create local file object
- FileObject localFile = manager.resolveFile(f.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();
- }
- }
- /*
- * args[0]: hostName
- * args[1]: username
- * args[2]: password
- * args[3]:localFilePath
- * args[4]: remoteFilePath
- */
- public static void main(String[] args) {
- if (args.length < 5)
- throw new RuntimeException(
- "Error. Please enter "
- + "args[0]: hostName, args[1]: username, args[2]: password, "
- + "args[3]: localFilePath, args[4]: remoteFilePath.");
-
- upload(args[0], args[1], args[2], args[3], args[4]);
- }
- public static void download(String hostName, String username,
- String password, String localFilePath, String remoteFilePath) {
-
- StandardFileSystemManager manager = new StandardFileSystemManager();
-
- try {
- manager.init();
-
- String downloadFilePath = localFilePath.substring(0,
- localFilePath.lastIndexOf("."))
- + "_downlaod_from_sftp"
- + localFilePath.substring(localFilePath.lastIndexOf("."),
- localFilePath.length());
-
- // Create local file object
- FileObject localFile = manager.resolveFile(downloadFilePath);
-
- // 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 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 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();
- }
- }
- package sftp.sample;
-
- import java.io.File;
-
- import org.apache.commons.vfs.FileObject;
- import org.apache.commons.vfs.FileSystemException;
- import org.apache.commons.vfs.FileSystemOptions;
- import org.apache.commons.vfs.Selectors;
- import org.apache.commons.vfs.impl.StandardFileSystemManager;
- import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
-
- public class Main {
-
- /*
- * args[0]: hostName
- * args[1]: username
- * args[2]: password
- * args[3]: localFilePath
- * args[4]: remoteFilePath
- */
- public static void main(String[] args) {
- if (args.length < 5)
- throw new RuntimeException(
- "Error. Please enter "
- + "args[0]: hostName, args[1]: username, args[2]: password, "
- + "args[3]: localFilePath, args[4]: remoteFilePath.");
-
- upload(args[0], args[1], args[2], args[3], args[4]);
- exist(args[0], args[1], args[2], args[4]);
- download(args[0], args[1], args[2], args[3], args[4]);
- delete(args[0], args[1], args[2], args[4]);
- }
-
- public static void upload(String hostName, String username,
- String password, String localFilePath, String remoteFilePath) {
-
- File f = new File(localFilePath);
- if (!f.exists())
- throw new RuntimeException("Error. Local file not found");
-
- StandardFileSystemManager manager = new StandardFileSystemManager();
-
- try {
- manager.init();
-
- // Create local file object
- FileObject localFile = manager.resolveFile(f.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 void download(String hostName, String username,
- String password, String localFilePath, String remoteFilePath) {
-
- StandardFileSystemManager manager = new StandardFileSystemManager();
-
- try {
- manager.init();
-
- String downloadFilePath = localFilePath.substring(0,
- localFilePath.lastIndexOf("."))
- + "_downlaod_from_sftp"
- + localFilePath.substring(localFilePath.lastIndexOf("."),
- localFilePath.length());
-
- // Create local file object
- FileObject localFile = manager.resolveFile(downloadFilePath);
-
- // 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();
- }
- }
-
- 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) {
- // result: "sftp://user:123456@domainname.com/resume.pdf
- 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, true);
-
- // Timeout is count by Milliseconds
- SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
-
- return opts;
- }
-
- }