为什么我的return语句返回null结果?

时间:2018-05-02 16:20:43

标签: spring sftp

我正在尝试连接到远程计算机并显示特定目录中的某些文件。

问题是,当我测试我的函数时,它返回一个null结果,我想要做的是显示文件名。

这是我的代码:

@Service
public class SftpClientImpl implements SftpClient {


    private LsEntry entry;

    @Override
    public LsEntry connectToServer() {
             String  SFTPHOST = "xxxxx";
             int   SFTPPORT = 22;
             String  SFTPUSER = "xxx";
             String  SFTPPASS = "xxxxx";
             String SFTPWORKINGDIR = "/dir/dir2/dir3";

            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 entry;
        } 
}

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<LsEntry> getDirectories() {
        LsEntry entry = sftpClient.connectToServer();
        return new ResponseEntity<>(entry, HttpStatus.OK);
    }

知道为什么这不起作用吗?

2 个答案:

答案 0 :(得分:1)

pred @Int () == False pred @Char () == True 为null,因为它的值仅包含在for循环中,并且实际上被声明两次(一次使用私有类作用域,一次使用for循环中的本地作用域)。

我建议更正变量声明并测试连接和文件名打印。如果它仍然无法正常工作,请在已知的工作弹簧端点内进行尝试。如果它按预期打印您的目录,而不是移动到其自己的端点,然后重试。这样做有助于缩小问题的范围。

我在过去几年中使用以下代码连接并打印文件名,并且很大程度上基于JSCH提供的示例代码:

SELECT XMLELEMENT("Columns", 
       XMLAGG(XMLELEMENT("Column", CNAME) ORDER BY CNAME)) AS DATA
FROM   (
  SELECT CNAME
  FROM   Col
  ORDER BY CNAME
  OFFSET 0 ROWS
  FETCH NEXT 100 ROWS ONLY
);

答案 1 :(得分:0)

这就是我解决问题的方法:) 我纠正了我的变量声明并且效果很好,就像你告诉我的那样:)谢谢

@Override
public LsEntry connectToServer() {
    String HOST = "xxxxx";
         int  PORT = 22;
         String  USER = "xxx";
         String  PASS = "xxxxx";
         String DIR = "/dir/dir2/dir3";

    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    // LsEntry declaration 
    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;
}