官网下载网站

JSch :http://www.jcraft.com/jsch/     sftp文件上传下载


http://search.maven.org/#search%7Cga%7C1%7C     下载maven管理的依赖jar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 jsch下载 sftp 文件的代码示例: ```java import com.jcraft.jsch.*; import java.io.*; public class SftpDownloader { public static void main(String[] args) { String host = "sftp.example.com"; int port = 22; String username = "username"; String password = "password"; String remoteFilePath = "/path/to/remote/file.txt"; String localFilePath = "C:\\path\\to\\local\\file.txt"; JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); InputStream inputStream = channelSftp.get(remoteFilePath); OutputStream outputStream = new FileOutputStream(localFilePath); byte[] buffer = new byte[1024]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); channelSftp.disconnect(); session.disconnect(); System.out.println("File downloaded successfully!"); } catch (JSchException | SftpException | IOException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们首先建立了一个与 SFTP 服务器的连接。然后我们打开了一个 SFTP 渠道,并使用 `get` 方法从服务器下载文件。最后,我们将从服务器下载的文件保存到本地磁盘。 请注意,这个示例假定你已经安装了 jsch 库并将其添加到类路径中。如果你使用 Maven 或 Gradle 等构建工具,请在构建文件中添加 jsch 依赖项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值