1. 概述
這篇文章將介紹如何用 Java 在遠程服務器上通過 SFTP 上傳和下載文件。
接下來將使用三種不同的開發庫實現,分別是 JSch、SSHJ 和 Apache Commons VFS。
2. 使用 JSch
首先,讓我們看看如何使用 JSch 在遠程服務器上實現文件上傳和下載。
2.1. Maven 配置
在項目 pom.xml 中添加 jsch 依賴:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
在 Maven 中央倉庫可以找到 jsch 的最新版本。
2.2. 配置 JSch
現在開始配置 JSch。
JSch 支持密碼或公共密鑰認證訪問遠程服務器。下面的示例採用密碼認證:
private ChannelSftp setupJsch() throws JSchException {
JSch jsch = new JSch();
jsch.setKnownHosts("/Users/john/.ssh/known_hosts");
Session jschSession = jsch.getSession(username, remoteHost);
jschSession.setPassword(password);
jschSession.connect();
return (ChannelSftp) jschSession.openChannel("sftp");
}
在這個示例中,remoteHost 表示成服務器的名稱或 IP 地址(例如 example.com)。在測試中可以像下面這樣定義:
private String remoteHost = "HOST_NAME_HERE";
private String username = "USERNAME_HERE";
private String password = "PASSWORD_HERE";
還以用下面的命令生成 known_hosts 文件:
ssh-keyscan -H -t rsa REMOTE_HOSTNAME >> known_hosts
2.3. 使用 JSch 上傳文件
調用ChannelSftp.put() 可以上傳文件到遠程服務器:
@Test
public void whenUploadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
ChannelSftp channelSftp = setupJsch();
channelSftp.connect();
String localFile = "src/main/resources/sample.txt";
String remoteDir = "remote_sftp_test/";
channelSftp.put(localFile, remoteDir + "jschFile.txt");
channelSftp.exit();
}
在這個示例中,第一個參數代表要上傳的本地文件,比如 src/main/resources/sample.txt;remoteDir 表示待上傳的遠程服務器路徑。
2.4. 使用 JSch 下載文件
還可以使用 ChannelSftp.get() 從遠程服務器上下載文件。
@Test
public void whenDownloadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
ChannelSftp channelSftp = setupJsch();
channelSftp.connect();
String remoteFile = "welcome.txt";
String localDir = "src/main/resources/";
channelSftp.get(remoteFile, localDir + "jschFile.txt");
channelSftp.exit();
}
remoteFile 表示要要下載的文件路徑,localDir 表示下載到本地的文件路徑。
3. 使用 SSHJ
接下來,我們將使用 SSHJ 庫在遠程服務器上實現文件上傳和下載。
3.1. Maven 配置
首先,在項目 pom.xml 中添加依賴:
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>sshj</artifactId>
<version>0.27.0</version>
</dependency>
在 Maven 中央倉庫可以找到 sshj 的最新版本。
3.2. 配置 SSHJ
接下來配置 SSHClient。
SSHJ 同樣支持密碼或公共密鑰認證訪問遠程服務器。下面的示例採用密碼認證:
private SSHClient setupSshj() throws IOException {
SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());
client.connect(remoteHost);
client.authPassword(username, password);
return client;
}
3.3. 使用 SSHJ 上傳文件
與 JSch 類似,調用 SFTPClient.put() 可以上傳文件到遠程服務器:
@Test
public void whenUploadFileUsingSshj_thenSuccess() throws IOException {
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.put(localFile, remoteDir + "sshjFile.txt");
sftpClient.close();
sshClient.disconnect();
}
這裏定義兩個新變量:
private String localFile = "src/main/resources/input.txt";
private String remoteDir = "remote_sftp_test/";
3.4. 使用 SSHJ 下載文件
同樣,這裏用 SFTPClient.get() 從遠程服務器上下載文件。
@Test
public void whenDownloadFileUsingSshj_thenSuccess() throws IOException {
SSHClient sshClient = setupSshj();
SFTPClient sftpClient = sshClient.newSFTPClient();
sftpClient.get(remoteFile, localDir + "sshjFile.txt");
sftpClient.close();
sshClient.disconnect();
}
添加兩個前面用到的變量:
private String remoteFile = "welcome.txt";
private String localDir = "src/main/resources/";
4. 使用 Apache Commons VFS
最後,使用 Apache Commons VFS 與遠程服務器傳送文件。
實際上,Apache Commons VFS 內部使用了 JSch 庫。
4.1. Maven 配置
需要在項目 pom.xml 中添加 commons-vfs2 依賴:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.4</version>
</dependency>
在 Maven 中央倉庫可以找到 commons-vfs2 的最新版本。
4.2. 使用 Apache Commons VFS 下載文件
Apache Commons VFS 的用法略有不同。
用FileSystemManager爲目標文件創建 FileObjects 對象,然後發送。
在這個示例中,調用 FileObject.copyFrom() 上傳文件。
@Test
public void whenUploadFileUsingVfs_thenSuccess() throws IOException {
FileSystemManager manager = VFS.getManager();
FileObject local = manager.resolveFile(
System.getProperty("user.dir") + "/" + localFile);
FileObject remote = manager.resolveFile(
"sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteDir + "vfsFile.txt");
remote.copyFrom(local, Selectors.SELECT_SELF);
local.close();
remote.close();
}
注意:本地文件應該使用絕對路徑,遠程文件路徑應該以sftp://username:password@remoteHost 開始。
4.3. 使用 Apache Commons VFS 下載文件
從遠程服務器上下載文件用法類似,還是用 FileObject.copyFrom() 從 remoteFile 拷貝到localFile。
@Test
public void whenDownloadFileUsingVfs_thenSuccess() throws IOException {
FileSystemManager manager = VFS.getManager();
FileObject local = manager.resolveFile(
System.getProperty("user.dir") + "/" + localDir + "vfsFile.txt");
FileObject remote = manager.resolveFile(
"sftp://" + username + ":" + password + "@" + remoteHost + "/" + remoteFile);
local.copyFrom(remote, Selectors.SELECT_SELF);
local.close();
remote.close();
}
5. 總結
這篇文章中,我們學習瞭如何用 Java 從遠程 SFTP 服務器上載和下載文件。爲此,我們使用了 JSch、SSHJ 和 Apache Commons VFS 多個開發庫。