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. 文件保存地址

程序yuan
原文地址:https://www.cnblogs.com/ooyhao/p/11297338.html