如何通過SFTP從服務器檢索文件?我試圖使用SFTP(而不是FTPS)使用Java從服務器上檢索文件。我該怎么做?
3 回答

qq_遁去的一_1
TA貢獻1725條經驗 獲得超8個贊
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(); } }}

鴻蒙傳說
TA貢獻1865條經驗 獲得超7個贊
下面是一個使用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);
添加回答
舉報
0/150
提交
取消