import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import java.io.BufferedOutputStream; import java.io.FileOutputStream; public class SftpClient implements AutoCloseable { private Session session; private ChannelSftp channelSftp; public SftpClient(String host ,int port , String user ,String passwd ) throws JSchException { //初始化Session 和 ChannelSftp JSch jSch = new JSch(); session = jSch.getSession(user, host, port); session.setConfig("StrickHostKeyCHecking","no"); session.setPassword(passwd); session.connect(); // channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); } public void downloadFile(String remmoteFile ,String localFile ){ //文件下载逻辑 try(BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(localFile),1024 )){ channelSftp.get(remmoteFile ,bufferedOutputStream); }catch (Exception e){ //异常 } } @Override public void close() throws Exception { if (channelSftp != null) { channelSftp.disconnect(); //sftp 连接关闭 } if (session != null){ session.disconnect(); //自动关闭会话 } }
sftpClient 自己实现了下语法糖的写法,封装了JSCH对象
最新推荐文章于 2024-11-01 14:48:20 发布