com.jcraft.jsch.ChannelSftp.ls()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(1496)

本文整理了Java中com.jcraft.jsch.ChannelSftp.ls()方法的一些代码示例,展示了ChannelSftp.ls()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ChannelSftp.ls()方法的具体详情如下:
包路径:com.jcraft.jsch.ChannelSftp
类名称:ChannelSftp
方法名:ls

ChannelSftp.ls介绍

[英]lists the contents of a remote directory.
[中]列出远程目录的内容。

代码示例

代码示例来源:origin: looly/hutool

/**
 * 遍历某个目录下所有文件或目录,不会递归遍历
 * 
 * @param path 遍历某个目录下所有文件或目录
 * @param filter 文件或目录过滤器,可以实现过滤器返回自己需要的文件或目录名列表
 * @return 目录或文件名列表
 * @since 4.0.5
 */
public List<String> ls(String path, final Filter<LsEntry> filter) {
  final List<String> fileNames = new ArrayList<>();
  try {
    channel.ls(path, new LsEntrySelector() {
      @Override
      public int select(LsEntry entry) {
        String fileName = entry.getFilename();
        if (false == StrUtil.equals(".", fileName) && false == StrUtil.equals("..", fileName)) {
          if (null == filter || filter.accept(entry)) {
            fileNames.add(entry.getFilename());
          }
        }
        return CONTINUE;
      }
    });
  } catch (SftpException e) {
    throw new JschRuntimeException(e);
  }
  return fileNames;
}

代码示例来源:origin: looly/hutool

/**
 * 遍历某个目录下所有文件或目录,不会递归遍历
 * 
 * @param path 遍历某个目录下所有文件或目录
 * @param filter 文件或目录过滤器,可以实现过滤器返回自己需要的文件或目录名列表
 * @return 目录或文件名列表
 * @since 4.0.5
 */
public List<String> ls(String path, final Filter<LsEntry> filter) {
  final List<String> fileNames = new ArrayList<>();
  try {
    channel.ls(path, new LsEntrySelector() {
      @Override
      public int select(LsEntry entry) {
        String fileName = entry.getFilename();
        if (false == StrUtil.equals(".", fileName) && false == StrUtil.equals("..", fileName)) {
          if (null == filter || filter.accept(entry)) {
            fileNames.add(entry.getFilename());
          }
        }
        return CONTINUE;
      }
    });
  } catch (SftpException e) {
    throw new JschRuntimeException(e);
  }
  return fileNames;
}

代码示例来源:origin: pentaho/pentaho-kettle

public String[] dir() throws KettleJobException {
 String[] fileList = null;
 try {
  java.util.Vector<?> v = c.ls( "." );
  java.util.Vector<String> o = new java.util.Vector<String>();
  if ( v != null ) {
   for ( int i = 0; i < v.size(); i++ ) {
    Object obj = v.elementAt( i );
    if ( obj != null && obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry ) {
     LsEntry lse = (com.jcraft.jsch.ChannelSftp.LsEntry) obj;
     if ( !lse.getAttrs().isDir() ) {
      o.add( lse.getFilename() );
     }
    }
   }
  }
  if ( o.size() > 0 ) {
   fileList = new String[o.size()];
   o.copyInto( fileList );
  }
 } catch ( SftpException e ) {
  throw new KettleJobException( e );
 }
 return fileList;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

sftpFiles = (Vector<LsEntry>) client.ls(absolute.toUri().getPath());
} catch (SftpException e) {
 throw new IOException(e);

代码示例来源:origin: apache/incubator-gobblin

@Override
public List<String> ls(String path) throws FileBasedHelperException {
 try {
  List<String> list = new ArrayList<>();
  ChannelSftp channel = getSftpChannel();
  Vector<LsEntry> vector = channel.ls(path);
  for (LsEntry entry : vector) {
   list.add(entry.getFilename());
  }
  channel.disconnect();
  return list;
 } catch (SftpException e) {
  throw new FileBasedHelperException("Cannot execute ls command on sftp connection", e);
 }
}

代码示例来源:origin: apache/nifi

vector = sftp.ls(fullPath);
} catch (final SftpException e) {

代码示例来源:origin: org.apache.hadoop/hadoop-common

Vector<LsEntry> sftpFiles;
try {
 sftpFiles = (Vector<LsEntry>) client.ls(pathName);
} catch (SftpException e) {
 throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));

代码示例来源:origin: jphp-group/jphp

@Signature
@SuppressWarnings("unchecked")
public Memory ls(Environment env, String path, @Nullable Invoker lsSelector) throws SftpException {
  Vector<ChannelSftp.LsEntry> ls = new Vector<>();
  ArrayMemory result = ArrayMemory.createListed(10);
  getWrappedObject().ls(path, new ChannelSftp.LsEntrySelector() {
    @Override
    public int select(ChannelSftp.LsEntry l) {
      ArrayMemory entry = ArrayMemory.createHashed();
      entry.put("name", l.getFilename());
      entry.put("longname", l.getLongname());
      entry.put("attrs", ArrayMemory.ofNullableBean(env, l.getAttrs()));
      if (lsSelector != null && lsSelector.callAny(entry).toBoolean()) {
        return ChannelSftp.LsEntrySelector.BREAK;
      }
      result.add(entry);
      return ChannelSftp.LsEntrySelector.CONTINUE;
    }
  });
  return result.toConstant();
}

代码示例来源:origin: apache/nifi

sftp.ls(".", filter);
} else {
  sftp.ls(path, filter);

代码示例来源:origin: looly/hutool

list = channel.ls(channel.pwd());
} catch (SftpException e) {
  throw new JschRuntimeException(e);

代码示例来源:origin: looly/hutool

list = channel.ls(channel.pwd());
} catch (SftpException e) {
  throw new JschRuntimeException(e);

代码示例来源:origin: dadoonet/fscrawler

@Override
public boolean exists(String dir) {
  try {
    sftp.ls(dir);
  } catch (Exception e) {
    return false;
  }
  return true;
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public LsEntry[] list(String path) throws IOException {
  Assert.state(this.channel != null, "session is not connected");
  try {
    Vector<?> lsEntries = this.channel.ls(path);
    if (lsEntries != null) {
      LsEntry[] entries = new LsEntry[lsEntries.size()];
      for (int i = 0; i < lsEntries.size(); i++) {
        Object next = lsEntries.get(i);
        Assert.state(next instanceof LsEntry, "expected only LsEntry instances from channel.ls()");
        entries[i] = (LsEntry) next;
      }
      return entries;
    }
  }
  catch (SftpException e) {
    throw new NestedIOException("Failed to list files", e);
  }
  return new LsEntry[0];
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

@Override
public Collection<DirEntry> ls(String path) throws IOException {
  return map(() -> {
    List<DirEntry> result = new ArrayList<>();
    for (Object e : ftp.ls(path)) {
      ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) e;
      result.add(new DirEntry() {
        @Override
        public String getFilename() {
          return entry.getFilename();
        }
        @Override
        public long getModifiedTime() {
          return entry.getAttrs().getMTime();
        }
        @Override
        public boolean isDirectory() {
          return entry.getAttrs().isDir();
        }
      });
    }
    return result;
  });
}

代码示例来源:origin: dadoonet/fscrawler

@SuppressWarnings("unchecked")
@Override
public Collection<FileAbstractModel> getFiles(String dir) throws Exception {
  logger.debug("Listing local files from {}", dir);
  Vector<ChannelSftp.LsEntry> ls;
  ls = sftp.ls(dir);
  if (ls == null) return null;
  Collection<FileAbstractModel> result = new ArrayList<>(ls.size());
  // Iterate other files
  // We ignore here all files like . and ..
  result.addAll(ls.stream().filter(file -> !".".equals(file.getFilename()) &&
      !"..".equals(file.getFilename()))
      .map(file -> toFileAbstractModel(dir, file))
      .collect(Collectors.toList()));
  logger.debug("{} local files found", result.size());
  return result;
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public SftpSession getSession() {
  if (this.sftpEntries.size() == 0) {
    this.init();
  }
  try {
    ChannelSftp channel = mock(ChannelSftp.class);
    String[] files = new File("remote-test-dir").list();
    for (String fileName : files) {
      when(channel.get("remote-test-dir/" + fileName))
          .thenReturn(new FileInputStream("remote-test-dir/" + fileName));
    }
    when(channel.ls("remote-test-dir")).thenReturn(sftpEntries);
    when(jschSession.openChannel("sftp")).thenReturn(channel);
    return SftpTestSessionFactory.createSftpSession(jschSession);
  }
  catch (Exception e) {
    throw new RuntimeException("Failed to create mock sftp session", e);
  }
}

代码示例来源:origin: spring-projects/spring-integration

sftpEntries.add(lsEntry);
when(channel.ls("remote-test-dir/")).thenReturn(sftpEntries);

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project

@Override
public synchronized boolean exists(String parent, String name) throws RemoteException {
  String fullPath = parent + "/" + name; // NOI18N
  try {
    sftpClient.ls(fullPath);
    return true;
  } catch (SftpException ex) {
    LOGGER.log(Level.FINE, "Error while checking existence of " + fullPath, ex);
  }
  return false;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project

private ChannelSftp.LsEntry getFile(String path) throws SftpException {
  assert Thread.holdsLock(this);
  assert path != null && path.trim().length() > 0;
  @SuppressWarnings("unchecked")
  List<ChannelSftp.LsEntry> files = sftpClient.ls(path);
  // in fact, the size of the list should be exactly 1
  LOGGER.fine(String.format("Exactly 1 file should be found for %s; found %d", path, files.size()));
  if (files.size() > 0) {
    return files.get(0);
  }
  return null;
}

代码示例来源:origin: stackoverflow.com

ChannelSftp sftpChannel = (ChannelSftp) channel;

try {
  Vector ls=sftpChannel.ls("/home/abc/Desktop");
  String[] strings = new String[ls.size];

  for(int i = 0; i < ls.size(); i++) {
    strings[i] = sftpChannel.pwd() + "/" + (((LsEntry)ls.get(i)).getFilename());
  }

  t.post(new Runnable() {
    public void run() { t.setText(strings.toString()); }
  });

  } catch (SftpException e1) { }

相关文章

微信公众号

最新文章

更多

ChannelSftp类方法