无需密码或私钥,Java实现SFTP连接与文件上传方案咨询
实现无需手动输入密码/私钥的Java SFTP连接
嘿,我完全懂你的困惑——FileZilla能悄咪咪自动登录,但Java SSH库却总要你填密码或私钥,这反差确实让人挠头。其实核心原因是:FileZilla会自动扫描你本地用户目录下的标准SSH私钥文件(比如~/.ssh/id_rsa、~/.ssh/id_dsa这类默认命名的密钥),而你用的JSch、Maverick这些库默认不会做这件事,得手动告诉它们私钥在哪才行。
下面给你具体的实现方案,先从最常用的JSch说起:
用JSch实现自动加载默认私钥
只要你的私钥是无密码的(和FileZilla登录的情况一致),直接指定私钥路径就能完成认证,不用输入密码。
import com.jcraft.jsch.*; import java.io.FileInputStream; import java.io.IOException; public class SftpAutoLoginUploader { public static void main(String[] args) { String username = "your-username"; String hostname = "your-sftp-host"; int port = 22; // 默认SFTP端口 String localFile = "/path/to/your/local/file.txt"; String remoteDest = "/remote/folder/to/save/file.txt"; JSch jsch = new JSch(); Session session = null; ChannelSftp sftpChannel = null; try { // 加载用户目录下的默认私钥(比如id_rsa) String privateKeyPath = System.getProperty("user.home") + "/.ssh/id_rsa"; jsch.addIdentity(privateKeyPath); // 创建会话并配置 session = jsch.getSession(username, hostname, port); java.util.Properties config = new java.util.Properties(); // 生产环境建议不要关闭主机密钥检查,最好把目标主机加到known_hosts里 config.put("StrictHostKeyChecking", "no"); session.setConfig(config); // 连接会话(私钥无密码,不用setPassword) session.connect(); // 打开SFTP通道并上传文件 sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.connect(); try (FileInputStream fis = new FileInputStream(localFile)) { sftpChannel.put(fis, remoteDest); System.out.println("文件上传成功!"); } } catch (JSchException | SftpException | IOException e) { e.printStackTrace(); } finally { // 收尾关闭资源 if (sftpChannel != null && sftpChannel.isConnected()) { sftpChannel.disconnect(); } if (session != null && session.isConnected()) { session.disconnect(); } } } }
关键细节:
jsch.addIdentity(privateKeyPath):这一步就是模拟FileZilla的自动查找逻辑,告诉JSch用哪个私钥文件。如果你的私钥不是默认的id_rsa,改成对应的路径就行。- 生产环境别偷懒关闭
StrictHostKeyChecking,把目标服务器的主机密钥加到~/.ssh/known_hosts里更安全。
用Maverick SFTP实现类似功能
如果你偏好Maverick库,逻辑也是一样的,手动加载私钥文件即可:
import com.sshtools.client.SshClient; import com.sshtools.client.sftp.SftpClient; import com.sshtools.common.publickey.FilePrivateKeyAuthentication; import com.sshtools.common.publickey.PrivateKeyFile; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class MaverickSftpAutoLogin { public static void main(String[] args) { String username = "your-username"; String hostname = "your-sftp-host"; int port = 22; String localFile = "/path/to/your/local/file.txt"; String remoteDest = "/remote/folder/to/save/file.txt"; try (SshClient client = new SshClient(hostname, port)) { // 加载本地私钥 String privateKeyPath = System.getProperty("user.home") + "/.ssh/id_rsa"; PrivateKeyFile keyFile = PrivateKeyFile.parse(new File(privateKeyPath)); // 私钥无密码,第二个参数传null就行 client.authenticate(new FilePrivateKeyAuthentication(username, keyFile, null)); // 上传文件 try (SftpClient sftp = client.openSftpClient(); FileInputStream fis = new FileInputStream(localFile)) { sftp.put(fis, remoteDest); System.out.println("文件上传成功!"); } } catch (IOException e) { e.printStackTrace(); } } }
为啥之前的尝试失败?
你之前用这些库的时候,大概率没显式指定私钥路径,库默认会尝试密码认证,所以才会弹出要求输入密码的提示。只要告诉库要使用本地的默认私钥,就能复刻FileZilla的无密码登录体验啦。
内容的提问来源于stack exchange,提问作者Raditz
相关产品推荐
相关产品推荐

