java使用ftp上傳下載


1. java使用ftp上傳下載

1.1. 生成known_hosts文件

ssh-keyscan -H -t rsa 120.79.167.88 >> known_hosts

1.2. JSch方式

1.2.1. Maven 配置

<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.55</version>
</dependency>

1.2.2. 配置Jsch

JSch 支持密碼或公共密鑰認證訪問遠程服務器。下面的示例采用密碼認證:

private ChannelSftp setupJsch() throws JSchException {  
    JSch jsch = new JSch();  
    jsch.setKnownHosts("C:/Users/5161/Desktop/known_hosts");  //前面生成好的文件
    Session jschSession = jsch.getSession("root", "120.79.167.xxx");//username,host
    jschSession.setPassword("123456");//密碼就是服務器登錄密碼
    jschSession.connect();  
    return (ChannelSftp) jschSession.openChannel("sftp");  
}

1.2.3. 使用Jsch上傳文件

@Test
public void whenUploadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
    ChannelSftp channelSftp = setupJsch();
    channelSftp.connect();
    String localFile = "C:/Users/5161/Desktop/java開發工程師_陽浩_137-5585-4643.pdf";
    String remoteDir = "/data/";
    channelSftp.put(localFile, remoteDir + "137-5585-4643.pdf");
    channelSftp.exit();
}

1.2.4. 使用Jsch下載文件

@Test
public void whenDownloadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
    ChannelSftp channelSftp = setupJsch();
    channelSftp.connect();
    String remoteFile = "/data/137-5585-4643.pdf";
    String localDir = "D:/";
    channelSftp.get(remoteFile, localDir + "137-5585-4643.pdf");
    channelSftp.exit();
}

1.2.5. 文件保存地址

1.3. SSHJ方式

1.3.1. Maven 配置

<dependency>
    <groupId>com.hierynomus</groupId>
    <artifactId>sshj</artifactId>
    <version>0.27.0</version>
</dependency>

1.3.2. 配置 SSHJ

SSHJ 同樣支持密碼或公共密鑰認證訪問遠程服務器。下面的示例采用密碼認證:

private SSHClient setupSshj() throws IOException {
    SSHClient client = new SSHClient();
    client.addHostKeyVerifier(new PromiscuousVerifier());
    client.connect("120.79.167.xxx");
    client.authPassword("root", "123456");
    return client;
}

1.3.3. 使用 SSHJ 上傳文件

@Test
public void whenUploadFileUsingSshj_thenSuccess() throws IOException {
    SSHClient sshClient = setupSshj();
    SFTPClient sftpClient = sshClient.newSFTPClient();
    sftpClient.put("C:/Users/5161/Desktop/java開發工程師_陽浩_137-5585-4643.pdf", "/data/" + "137-5585-0001.pdf");
    sftpClient.close();
    sshClient.disconnect();
}

1.3.4. 使用 SSHJ 下載文件

@Test
public void whenDownloadFileUsingSshj_thenSuccess() throws IOException {
    SSHClient sshClient = setupSshj();
    SFTPClient sftpClient = sshClient.newSFTPClient();
    sftpClient.get("/data/137-5585-0001.pdf", "d:/" + "137-5585-0001.pdf");
    sftpClient.close();
    sshClient.disconnect();
}

1.3.5. 文件保存地址

1.4. Apache Commons VFS

1.4.1. Maven 配置

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-vfs2</artifactId>
    <version>2.4</version>
</dependency>

1.4.2. 使用 Apache Commons VFS 下載文件

Apache Commons VFS 的用法略有不同。
用FileSystemManager為目標文件創建 FileObjects 對象,然后發送。
在這個示例中,調用 FileObject.copyFrom() 上傳文件。

/*存儲在/root/下*/
@Test
public void whenUploadFileUsingVfs_thenSuccess() throws IOException {
    FileSystemManager manager = VFS.getManager();
    System.out.println(System.getProperty("user.dir"));
//        FileObject local = manager.resolveFile(System.getProperty("user.dir") + "/" + "d:/137-5585-0001.pdf");
    FileObject local = manager.resolveFile( "d:\\137-5585-0001.pdf");
    FileObject remote = manager.resolveFile(
            "sftp://" + "root" + ":" + "123456" + "@" + "120.79.167.xxx" + "/" + "/data/" + "137-5585-0002.pdf");
    remote.copyFrom(local, Selectors.SELECT_SELF);
    local.close();
    remote.close();
}

注意:本地文件應該使用絕對路徑,遠程文件路徑應該以 sftp://username:password@remoteHost 開始。

1.4.3. 使用 Apache Commons VFS 下載文件

從遠程服務器上下載文件用法類似,還是用 FileObject.copyFrom() 從 remoteFile 拷貝到 localFile

@Test
public void whenDownloadFileUsingApacheVfs_thenSuccess() throws IOException {
    FileSystemManager manager = VFS.getManager();
    FileObject local = manager.resolveFile("d:/137-5585-00012.pdf");
    FileObject remote = manager.resolveFile("sftp://" + "root" + ":" + "123456" + "@" + "120.79.167.xxx" + "/" + "/data/137-5585-0002.pdf");
    local.copyFrom(remote, Selectors.SELECT_SELF);
    local.close();
    remote.close();
}

1.4.4. 文件保存地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM