如何通过SFTP从服务器检索文件?

如何通过SFTP从服务器检索文件?

我试图使用SFTP(而不是FTPS)使用Java从服务器上检索文件。我该怎么做?



繁华开满天机
浏览 1962回答 3
3回答

qq_遁去的一_1

下面是一个示例的完整源代码杰奇而不必担心ssh键检查。import com.jcraft.jsch.*;public class TestJSch {     public static void main(String args[]) {         JSch jsch = new JSch();         Session session = null;         try {             session = jsch.getSession("username", "127.0.0.1", 22);             session.setConfig("StrictHostKeyChecking", "no");             session.setPassword("password");             session.connect();             Channel channel = session.openChannel("sftp");             channel.connect();             ChannelSftp sftpChannel = (ChannelSftp) channel;             sftpChannel.get("remotefile.txt", "localfile.txt");             sftpChannel.exit();             session.disconnect();         } catch (JSchException e) {             e.printStackTrace();           } catch (SftpException e) {             e.printStackTrace();         }     }}

鸿蒙传说

下面是一个使用ApacheCommonVFS的示例:FileSystemOptions fsOptions = new FileSystemOptions();SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fsOptions, "no");FileSystemManager fsManager = VFS.getManager();String uri = "sftp://user:password@host:port/absolute-path";FileObject fo = fsManager.resolveFile(uri, fsOptions);
打开App,查看更多内容
随时随地看视频慕课网APP