Spring Boot + sshd-sftp:SSH 命令与文件传输实践
在现代分布式系统中,服务器间的远程操作与文件传输是常见需求。SSH作为一种安全的网络协议,为远程登录和文件传输提供了可靠保障。
前言
免费影视、动漫、音乐、游戏、小说资源长期稳定更新! 👉 点此立即查看 👈
在现代分布式系统中,服务器间的远程操作与文件传输是常见需求。SSH作为一种安全的网络协议,为远程登录和文件传输提供了可靠保障。
环境准备
sshd-sftp是Apache MINA项目旗下的SSH服务器组件,支持SSHv2协议,提供了丰富的API用于构建SSH客户端和服务器。相比传统的JSch库,sshd-sftp具有更活跃的社区维护和更完善的功能支持,尤其在处理大文件传输和并发连接时表现更优。
案例
效果图
图片
核心依赖
SSH 连接管理
建立和管理SSH连接是所有操作的基础,合理的连接池设计能有效提升系统性能。
创建SshClientUtil工具类管理连接生命周期:
import lombok.extern.slf4j.Slf4j;import org.apache.sshd.client.SshClient;import org.apache.sshd.client.session.ClientSession;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import java.io.IOException;@Slf4j@Componentpublic class SshClientUtil { @Value("${ssh.host:182.168.1.101}") private String host; @Value("${ssh.port:22}") private int port; @Value("${ssh.username:root}") private String username; @Value("${ssh.password:root}") private String password; @Value("${ssh.timeout:5000}") private int timeout; private SshClient client; private ClientSession session; /** * 建立SSH连接 */ public void connect() throws IOException { if (session != null && session.isOpen()) { return; } client = SshClient.setUpDefaultClient(); client.start(); session = client.connect(username, host, port) .verify(timeout) .getSession(); session.addPasswordIdentity(password); session.auth().verify(timeout); log.info("SSH连接成功"); } /** * 断开SSH连接 */ public void disconnect() throws IOException { if (session != null && session.isOpen()) { session.close(); } if (client != null && client.isStarted()) { client.stop(); } } public ClientSession getSession() throws IOException { if (session == null || session.isEmpty() ||session.isClosed()) { connect(); } return session; }}
远程命令执行
通过SSH协议执行远程命令是服务器管理的常用功能,需要处理命令输出和错误信息。
import org.apache.sshd.client.channel.ChannelExec;import org.apache.sshd.client.channel.ClientChannelEvent;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;import java.io.*;import java.util.ArrayList;import java.util.Arrays;import java.util.List;@Servicepublic class RemoteCommandService { @Autowired private SshClientUtil sshClientUtil; @Value("${ssh.charset:UTF-8}") private String charset; /** * 执行单条SSH命令 * * @param command 命令字符串 * @return 命令输出结果 */ public String executeCommand(String command) throws IOException { try (ChannelExec channel = sshClientUtil.getSession().createExecChannel(command)) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); channel.setOut(outputStream); channel.setErr(errorStream); channel.open(); channel.waitFor(Arrays.asList(ClientChannelEvent.CLOSED), 0); int exitStatus = channel.getExitStatus(); if (exitStatus != 0) { String errorMsg = new String(errorStream.toByteArray(), charset); throw new RuntimeException("命令执行失败: " + errorMsg); } return new String(outputStream.toByteArray(), charset); } } /** * 执行多条命令(按顺序执行) * * @param commands 命令列表 * @return 命令输出结果列表 */ public List
文件上传与下载
SFTP是基于SSH的安全文件传输协议,相比FTP具有更高的安全性。
import org.apache.sshd.sftp.client.SftpClient;import org.apache.sshd.sftp.client.SftpClientFactory;import org.apache.sshd.sftp.common.SftpException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;import java.io.*;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.ArrayList;import java.util.Arrays;import java.util.List;@Servicepublic class SftpFileService { @Autowired private SshClientUtil sshClientUtil; @Value("${ssh.charset:UTF-8}") private String charset; /** * 上传本地文件到远程服务器 * * @param localFilePath 本地文件路径 * @param remoteDir 远程目录路径 */ public void uploadFile(String localFilePath, String remoteDir) throws IOException { try (SftpClient client = SftpClientFactory.instance().createSftpClient(sshClientUtil.getSession()); SftpClient.CloseableHandle handle = client.open(remoteDir, SftpClient.OpenMode.Write, SftpClient.OpenMode.Create)) { // 上传文件 Path local = Paths.get(localFilePath); client.write(handle, 0, Files.readAllBytes(local)); } } /** * 从远程服务器下载文件 * * @param remoteFilePath 远程文件路径 * @param localDir 本地目录路径 */ public void downloadFile(String remoteFilePath, String localDir) throws IOException { try (SftpClient client = SftpClientFactory.instance().createSftpClient(sshClientUtil.getSession()); SftpClient.CloseableHandle handle = client.open(remoteFilePath, SftpClient.OpenMode.Read); OutputStream out = Files.newOutputStream(Paths.get(localDir))) { long size = client.stat(handle).getSize(); byte[] buffer = new byte[8192]; for (long offset = 0; offset < size; ) { int len = client.read(handle, offset, buffer, 0, buffer.length); out.write(buffer, 0, len); offset += len; } } } /** * 递归创建远程目录 */ private void mkdirs(SftpClient client, String dir) throws IOException { String[] dirs = dir.split("/"); String currentDir = ""; for (String d : dirs) { if (d.isEmpty()) continue; currentDir += "/" + d; try { client.stat(currentDir); // 检查目录是否存在 } catch (IOException e) { client.mkdir(currentDir); // 不存在则创建 } } }}
相关攻略
一、SSH反向连接内网主机详解 当目标主机处于防火墙或网络地址转换(NAT)设备后方时,传统的SSH直连方式往往无法奏效。此时,借助SSH反向连接技术,即可由内网主机主动向外网控制端发起连接,构建一条可靠的加密访问通道。这项技术的原理看似复杂,实际操作只需遵循清晰的“四步法”即可轻松完成。 首先,请
角色与核心任务 今天,我们来聊聊一个技术写作中常见却棘手的挑战:如何把一份充满AI腔调的冰冷文稿,打磨成一篇有血有肉、令人信服的专业文章。这并非简单的同义词替换,而是一项关于风格、温度与专业度平衡的艺术。 深度复盘:从 AI 文本到人类作品的转化路径 要达到理想的效果,背后的执行逻辑必须清晰且克制,
编辑 | 王凤枝沉寂9天之后,OpenClaw正式推送了号称里程碑的3 22大版本更新,但我们给普通用户的核心建议是,千万别急着点升级。3月22日,项目创始人彼得·斯坦伯格(Peter Steinb
新智元报道编辑:Aeneas【新智元导读】一夜爆红的ClawdBot,正在把无数公司和个人推向深渊:端口裸奔、无鉴权、可被远程接管。现在,暴力破解、数据清空已经真实发生了,这不是危言耸听。各位CEO
在现代分布式系统中,服务器间的远程操作与文件传输是常见需求。SSH作为一种安全的网络协议,为远程登录和文件传输提供了可靠保障。 前言在现代分布式系统中,服务器间的远程操作与文件传输是常见需求。SSH
热门专题
热门推荐
一、财务系统更换:一场不容有失的“心脏手术” 如果把企业比作一个生命体,那么财务系统就是它的“心脏”。这颗“心脏”一旦老化,更换就成了必须面对的课题。但这绝非一次简单的软件升级,而是一场精密、复杂、牵一发而动全身的“外科手术”。数据显示,超过70%的ERP(企业资源计划)项目实施未能完全达到预期,问
在企业数字化转型的浪潮中,模拟人工点击软件:从效率工具到智能伙伴 企业数字化转型的路上,绕不开一个话题:如何把那些重复、枯燥的电脑操作交给机器?模拟人工点击软件,正是因此而成为了提升效率、降低成本的得力助手。那么,市面上的这类软件到底有哪些?答案其实很清晰。它们大致可以归为三类:基础按键脚本、传统R
一、核心结论:AI智能体是通往AGI的必经之路 时间来到2026年,AI智能体这个词儿,早就跳出了PPT和实验室的范畴。它不再是飘在天上的技术概念,而是实实在在地成了驱动全球数字化转型的引擎。和那些只能一问一答的传统对话式AI不同,如今的AI智能体(Agent)本事可大多了:它们能自己规划任务步骤、
一、核心结论:AI智能体交互的“桥梁”是行动层 在AI智能体的标准架构里,它与外部系统打交道,关键靠的是“行动层”。可以这么理解:感知层是Agent的五官,决策层是它的大脑,而行动层,就是那双真正去执行和操作的手。这一层专门负责把大脑产出的抽象指令,“翻译”成外部系统能懂的语言,无论是调用一个API
一、核心结论:AI人设是智能体的“灵魂” 在构建AI应用时,一个核心问题摆在我们面前:如何写好AI智能体的人设描述?这个问题的答案,直接决定了智能体输出的专业度与用户端的信任感。业界实践表明,一个优秀的人设描述,离不开一个叫做RBGT的模型框架,它涵盖了角色、背景、目标和语气四个黄金维度。有研究数据





