使用java进行FTP服务器的登陆和操作
1. 导入所需依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
2. 登陆代码
ChannelSftp sftp = null;
String fileName = null;
try {
JSch jSch = new JSch();
if (ftp.getPrivateKey() != null) {
jSch.addIdentity(ftp.getPrivateKey());
}
Session session = jSch.getSession(ftpLoginInfoById.getUserName(), ftpLoginInfoById.getHost(), ftpLoginInfoById.getPort());
if (ftp.getPassword() != null) {
session.setPassword(ftp.getPassword());
}
session.setTimeout(100000);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
System.out.println("更目录");
} catch (Exception e) {
e.printStackTrace();
}
以上亲测可用