缺少return语句以使该方法有效

时间:2018-05-02 10:35:54

标签: spring sftp

我正在尝试通过sftp从远程服务器列出文件 我被困在这里: 我应该把它作为一个重新声明来使它工作^^'

@Override
public LsEntry connectToServer() {
         String  SFTPHOST = "xxxxx";
         int   SFTPPORT = 22;
         String  SFTPUSER = "xxxxx";
         String  SFTPPASS = "xxxx";
         String SFTPWORKINGDIR = "/xx/vvv/bbb/rrr";

        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;

        try{
            JSch jsch = new JSch();
            session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
            session.setPassword(SFTPPASS);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            channel = session.openChannel("sftp");
            channel.connect();
            System.out.println("Starting the session ..");
            channelSftp = (ChannelSftp)channel;
            channelSftp.cd(SFTPWORKINGDIR);
            Vector filelist = channelSftp.ls(SFTPWORKINGDIR);
            for(int i=0; i<filelist.size();i++){
                LsEntry entry = (LsEntry) filelist.get(i);
                System.out.println(entry.getFilename());
            }
            while(session != null){
                System.out.println("Killing the session");
                session.disconnect();
                System.exit(0);
            }

        }catch(Exception ex){
            ex.printStackTrace();
        }
        return WHAAT ;


    }

任何人都可以帮我这个吗? 我真的不知道该怎么做:( 提前谢谢

1 个答案:

答案 0 :(得分:0)

这是因为entry是null,因为它的值只包含在for循环中,

所以我更正了我的入场申报并且有效:)

LsEntry entry = null;
try {
    JSch jsch = new JSch();
    session = jsch.getSession(USER, HOST, PORT);
    session.setPassword(PASS);
    // the rest of the code
    //....
    //...
    for (int i = 0; i < filelist.size(); i++) {
        //cast
        entry = (LsEntry) filelist.get(i);
        System.out.println(((LsEntry) entry).getFilename());
    }
    while (session != null) {
        System.out.println("Killing the session");
        session.disconnect();
        System.exit(0);
    }

} catch (Exception ex) {
    ex.printStackTrace();
}
return (LsEntry) entry;