java jsch下载文件_JSch - Java实现的SFTP(文件下载详解篇) -【转载】

本文档展示了如何使用Java的JSch库从SFTP服务器下载文件。通过创建`SFTPChannel`类并配置SFTP连接详情,然后调用`get()`方法将远程文件保存到本地。提供了三种不同的文件下载方式:直接保存到本地文件、通过`OutputStream`写入和使用`InputStream`读取。
摘要由CSDN通过智能技术生成

packagecom.longyg.sftp;importjava.io.FileOutputStream;importjava.io.OutputStream;importjava.util.HashMap;importjava.util.Map;importcom.jcraft.jsch.ChannelSftp;importcom.jcraft.jsch.SftpATTRS;public classSFTPGetTest {publicSFTPChannel getSFTPChannel() {return newSFTPChannel();

}public static void main(String[] args) throwsException {

SFTPGetTest test= newSFTPGetTest();

Map sftpDetails = new HashMap();//设置主机ip,端口,用户名,密码

sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "10.9.167.55");

sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME,"root");

sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD,"arthur");

sftpDetails.put(SFTPConstants.SFTP_REQ_PORT,"22");

SFTPChannel channel=test.getSFTPChannel();

ChannelSftp chSftp= channel.getChannel(sftpDetails, 60000);

String filename= "/home/omc/ylong/sftp/INTPahcfg.tar.gz";

SftpATTRS attr=chSftp.stat(filename);long fileSize =attr.getSize();

String dst= "D:\\INTPahcfg.tar.gz";

OutputStream out= newFileOutputStream(dst);try{

chSftp.get(filename, dst,new FileProgressMonitor(fileSize)); //代码段1//chSftp.get(filename, out, new FileProgressMonitor(fileSize));//代码段2/*** 代码段3

*

InputStream is = chSftp.get(filename, new MyProgressMonitor());

byte[] buff = new byte[1024 * 2];

int read;

if (is != null) {

System.out.println("Start to read input stream");

do {

read = is.read(buff, 0, buff.length);

if (read > 0) {

out.write(buff, 0, read);

}

out.flush();

} while (read >= 0);

System.out.println("input stream read done.");

}*/}catch(Exception e) {

e.printStackTrace();

}finally{

chSftp.quit();

channel.closeChannel();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!针对您的问题,我可以给出以下解答: Java JSch 是一个用于远程执行命令和传输文件Java 库,通过 JSch,我们可以在 Java 应用程序中连接到远程主机,并执行远程主机上的命令。 针对您的问题,如果您在使用 Java JSch 调用 shell 脚本时,脚本执行后立即退出,可能是因为您的 Java 程序没有等待 shell 脚本执行完毕就立即退出了。 为了解决这个问题,您可以在调用 shell 脚本的代码中,使用 `ChannelExec.setPty(true)` 方法启用伪终端模式,并使用 `ChannelExec.setCommand(command + ";echo finished")` 方法在 shell 脚本执行完毕后输出一个 "finished" 的标识符。然后您可以使用 `ChannelExec.getInputStream()` 方法获取 shell 脚本的输出流,并等待输出流中包含 "finished" 标识符,表示 shell 脚本已经执行完毕,然后再关闭连接。 以下是一个示例代码: ``` JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setPty(true); channelExec.setCommand(command + ";echo finished"); channelExec.setInputStream(null); channelExec.setErrStream(System.err); InputStream inputStream = channelExec.getInputStream(); channelExec.connect(); byte[] tmp = new byte[1024]; while (true) { while (inputStream.available() > 0) { int i = inputStream.read(tmp, 0, 1024); if (i < 0) break; System.out.print(new String(tmp, 0, i)); } if (channelExec.isClosed()) { if (inputStream.available() > 0) continue; System.out.println("exit-status: " + channelExec.getExitStatus()); break; } try { Thread.sleep(1000); } catch (Exception ee) {} } channelExec.disconnect(); session.disconnect(); ``` 希望这个解答能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值