m0_37769150
m0_37769150
采纳率0%
2018-01-24 06:14

sftp遇到的一个bug,报错,急求大神解决

5

ChannelSftp.ls(String directory)方法,使用中会报异常:
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1720)
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1526)
Caused by: java.io.IOException: Pipe closed
at java.io.PipedInputStream.read(PipedInputStream.java:291)
at com.jcraft.jsch.Channel$MyPipedInputStream.updateReadSide(Channel.java:362)
at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1543)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • fengminfengmin fengminfengmin 3年前

    public static void downloadSftpFile(String ftpHost, String ftpUserName,

    String ftpPassword, int ftpPort, String ftpPath, String localPath,

    String fileName) throws JSchException {

    Session session = null;

    Channel channel = null;

        JSch jsch = new JSch();  
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort);  
        session.setPassword(ftpPassword);  
        session.setTimeout(100000);  
        Properties config = new Properties();  
        config.put("StrictHostKeyChecking", "no");  
        session.setConfig(config);  
        session.connect();  
    
        channel = session.openChannel("sftp");  
        channel.connect();  
        ChannelSftp chSftp = (ChannelSftp) channel;  
    
        String ftpFilePath = ftpPath + "/" + fileName;  
        String localFilePath = localPath + File.separatorChar + fileName;  
    
        try {  
            chSftp.get(ftpFilePath, localPath);  
        } catch (Exception e) {  
            e.printStackTrace();  
            logger.info("download error.");  
        } finally {  
            chSftp.quit();  
            channel.disconnect();  
            session.disconnect();  
        }  
    
    }  
    

    }

    参考以上代码 使用
    jsch-0.1.54.jar 可以成功

    
    
    点赞 评论 复制链接分享