Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 为什么我的return语句返回空结果?_Spring_Sftp - Fatal编程技术网

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

Spring 为什么我的return语句返回空结果?,spring,sftp,Spring,Sftp,我正在尝试连接到一台远程机器,并显示特定目录中的一些文件 问题是,当我测试我的函数时,它返回一个空结果,我要做的是显示文件名 这是我的密码: @Service public class SftpClientImpl implements SftpClient { private LsEntry entry; @Override public LsEntry connectToServer() { String SFTPHOST = "xx

我正在尝试连接到一台远程机器,并显示特定目录中的一些文件

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

这是我的密码:

@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;
        } 
}
@服务
公共类SFTPlientImpl实现SFTPlient{
私人入境;
@凌驾
公共LsEntry connectToServer(){
字符串SFTPHOST=“xxxxx”;
int-SFTPPORT=22;
字符串SFTPUSER=“xxx”;
字符串sftpass=“xxxxx”;
字符串SFTPWORKINGDIR=“/dir/dir2/dir3”;
会话=空;
通道=空;
ChannelSftp ChannelSftp=null;
试一试{
JSch JSch=新的JSch();
session=jsch.getSession(SFTPUSER、sftport、SFTPPORT);
session.setPassword(sftpass);
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
session.setConfig(config);
session.connect();
通道=会话.openChannel(“sftp”);
channel.connect();
System.out.println(“启动会话…”);
channelSftp=(channelSftp)信道;
渠道SFTP.cd(SFTPWORKINGDIR);
向量文件列表=channelSftp.ls(SFTPWORKINGDIR);

for(int i=0;i
条目
为空,因为它的值仅包含在for循环中,并且实际声明了两次(一次使用私有类范围,一次使用for循环中的本地范围)

我建议您更正变量声明并测试连接和文件名打印。如果仍然不起作用,请在已知的工作spring终结点内尝试。如果它按预期打印您的目录,请移动到自己的终结点并重试。这样做将有助于缩小问题的范围

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

  JSch jsch = new JSch();
  Session session;

  session = jsch.getSession(username, hostname, port);
  session.setConfig("StrictHostKeyChecking", "no");
  session.setPassword(password);
  session.connect();

  Channel channel = session.openChannel("sftp");
  channel.connect();
  ChannelSftp sftpChannel = (ChannelSftp) channel;

   //List our files within the directory
   Vector vv = sftpChannel.ls(srcDir);
   if (vv != null) {
       LOGGER.debug("We have a file listing!");
       for (int ii = 0; ii < vv.size(); ii++) {
           Object obj = vv.elementAt(ii);
           if (obj instanceof ChannelSftp.LsEntry) {
               LOGGER.debug("[" + ((ChannelSftp.LsEntry) obj).getFilename() + "]");

               if (ii < 1) { // empty directory contains entries for . and ..
                   continue;
               }

               String filename = ((ChannelSftp.LsEntry) obj).getFilename();
               filenames.add(filename);
               LOGGER.debug("filename is: {}", filename);
               ....
JSch-JSch=new-JSch();
会议;
session=jsch.getSession(用户名、主机名、端口);
session.setConfig(“StrictHostKeyChecking”、“no”);
session.setPassword(密码);
session.connect();
Channel=session.openChannel(“sftp”);
channel.connect();
ChannelSftp sftpChannel=(ChannelSftp)信道;
//在目录中列出我们的文件
向量vv=sftpChannel.ls(srcDir);
如果(vv!=null){
debug(“我们有一个文件列表!”);
对于(int ii=0;ii
这就是我解决问题的方法:) 我纠正了我的变量声明,它工作得很好,就像你告诉我的:)谢谢

@覆盖
公共LsEntry connectToServer(){
字符串HOST=“xxxxx”;
int端口=22;
字符串USER=“xxx”;
字符串PASS=“xxxxx”;
字符串DIR=“/DIR/dir2/dir3”;
会话=空;
通道=空;
ChannelSftp ChannelSftp=null;
//入境声明
LsEntry entry=null;
试一试{
JSch JSch=新的JSch();
session=jsch.getSession(用户、主机、端口);
session.setPassword(PASS);
//代码的其余部分
//....
//...
对于(int i=0;i
如何调用端点?@RequestMapping(“/log”)问题是,我已经连接到服务器,并列出了文件,但只有当我将其作为一个无效方法进行测试时,我才得到结果,但我真正想要的是返回文件列表,以便我可以在我的用户界面中显示它。如果您查看我的方法,您可以看到我也这样做:)
filenames
是一个arraylist@MartinPrikry我这样做只是为了确保我所做的是100%真实的,并分享我的更新:)啊,是的,我忘了批准他的答案,我会这么做:)
  JSch jsch = new JSch();
  Session session;

  session = jsch.getSession(username, hostname, port);
  session.setConfig("StrictHostKeyChecking", "no");
  session.setPassword(password);
  session.connect();

  Channel channel = session.openChannel("sftp");
  channel.connect();
  ChannelSftp sftpChannel = (ChannelSftp) channel;

   //List our files within the directory
   Vector vv = sftpChannel.ls(srcDir);
   if (vv != null) {
       LOGGER.debug("We have a file listing!");
       for (int ii = 0; ii < vv.size(); ii++) {
           Object obj = vv.elementAt(ii);
           if (obj instanceof ChannelSftp.LsEntry) {
               LOGGER.debug("[" + ((ChannelSftp.LsEntry) obj).getFilename() + "]");

               if (ii < 1) { // empty directory contains entries for . and ..
                   continue;
               }

               String filename = ((ChannelSftp.LsEntry) obj).getFilename();
               filenames.add(filename);
               LOGGER.debug("filename is: {}", filename);
               ....
@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;
}