@SuppressWarnings("unchecked") public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, SftpException, Exception { Session session = getSession(user, password, addr, port); Vector<LsEntry> lsVec=null; Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; try { lsVec=(Vector<LsEntry>)sftpChannel.ls(cwd); //sftpChannel.lpwd() } catch (Exception e) { e.printStackTrace(); throw e; } finally { sftpChannel.exit(); channel.disconnect(); session.disconnect(); } return lsVec; }
/** * Lists directory files on remote server. * @throws URISyntaxException * @throws JSchException * @throws SftpException */ private void listFiles() throws URISyntaxException, JSchException, SftpException { JSch jsch = new JSch(); JSch.setLogger(new JschLogger()); setupSftpIdentity(jsch); URI uri = new URI(sftpUrl); Session session = jsch.getSession(sshLogin, uri.getHost(), 22); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); System.out.println("Connected to SFTP server"); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; Vector<LsEntry> directoryEntries = sftpChannel.ls(uri.getPath()); for (LsEntry file : directoryEntries) { System.out.println(String.format("File - %s", file.getFilename())); } sftpChannel.exit(); session.disconnect(); }
/** * Convert the file information in LsEntry to a {@link FileStatus} object. * * * @param sftpFile * @param parentPath * @return file status * @throws IOException */ private FileStatus getFileStatus(ChannelSftp channel, LsEntry sftpFile, Path parentPath) throws IOException { SftpATTRS attr = sftpFile.getAttrs(); long length = attr.getSize(); boolean isDir = attr.isDir(); boolean isLink = attr.isLink(); if (isLink) { String link = parentPath.toUri().getPath() + "/" + sftpFile.getFilename(); try { link = channel.realpath(link); Path linkParent = new Path("/", link); FileStatus fstat = getFileStatus(channel, linkParent); isDir = fstat.isDirectory(); length = fstat.getLen(); } catch (Exception e) { throw new IOException(e); } } int blockReplication = 1; // Using default block size since there is no way in SFTP channel to know of // block sizes on server. The assumption could be less than ideal. long blockSize = DEFAULT_BLOCK_SIZE; long modTime = attr.getMTime() * 1000; // convert to milliseconds long accessTime = 0; FsPermission permission = getPermissions(sftpFile); // not be able to get the real user group name, just use the user and group // id String user = Integer.toString(attr.getUId()); String group = Integer.toString(attr.getGId()); Path filePath = new Path(parentPath, sftpFile.getFilename()); return new FileStatus(length, isDir, blockReplication, blockSize, modTime, accessTime, permission, user, group, filePath.makeQualified( this.getUri(), this.getWorkingDirectory())); }
private Vector<LsEntry> listEntries(final ChannelSftp channelSftp, final String path) throws SftpException { final Vector<LsEntry> vector = new Vector<LsEntry>(); LsEntrySelector selector = new LsEntrySelector() { public int select(LsEntry entry) { final String filename = entry.getFilename(); if (filename.equals(".") || filename.equals("..")) { return CONTINUE; } if (entry.getAttrs().isLink()) { vector.addElement(entry); } else if (entry.getAttrs().isDir()) { if (keepDirectory(filename)) { vector.addElement(entry); } } else { if (keepFile(filename)) { vector.addElement(entry); } } return CONTINUE; } }; channelSftp.ls(path, selector); return vector; }
@Override public EncFSFileInfo getFileInfo(String srcPath) throws IOException { if ("/".equals(getAbsolutePath(srcPath))) { //return the root file info return new EncFSFileInfo("/","/",true,0,0,true,true,true); } Vector<LsEntry> ls = null; try { String parentPath = getParentPath(getAbsolutePath(srcPath)); ls = getSession().ls(parentPath); } catch (SftpException e){ throw new RuntimeException(e); } for (LsEntry lsEntry: ls){ if (lsEntry.getFilename().equals(".") || lsEntry.getFilename().equals("..")) { continue; } String searchedFullPath = getAbsolutePath(srcPath); if (searchedFullPath.endsWith("/")) searchedFullPath=searchedFullPath.substring(0,searchedFullPath.length()-1); if (searchedFullPath.endsWith("/"+lsEntry.getFilename())) return LsEntry2EncFSFileInfo(getParentPath(getAbsolutePath(srcPath)),lsEntry); } return null; }
private EncFSFileInfo LsEntry2EncFSFileInfo(String parentPath, LsEntry file){ EncFSFileInfo result; try { String name = file.getFilename(); SftpATTRS attrs = file.getAttrs(); long mtime = attrs.getMTime(); boolean isDir = attrs.isDir(); long length = attrs.getSize(); String relativePath=this.getRelativePathFromAbsolutePath(parentPath); if (name.endsWith("/")) name=name.substring(0,name.length()-1); result = new EncFSFileInfo(name,relativePath,isDir,mtime,length,true,true,true); } catch (Exception e){ throw new RuntimeException(e); } return result; }
private List<FileBean> _buildFiles(Vector ls,String dir) throws Exception { if (ls != null && ls.size() >= 0) { List<FileBean> list = new ArrayList<FileBean>(); for (int i = 0; i < ls.size(); i++) { LsEntry f = (LsEntry) ls.get(i); String nm = f.getFilename(); if (nm.equals(".") || nm.equals("..")) continue; SftpATTRS attr = f.getAttrs(); FileBean fileBean=new FileBean(); if (attr.isDir()) { fileBean.setDir(true); } else { fileBean.setDir(false); } fileBean.setAttrs(attr); fileBean.setFilePath(dir); fileBean.setFileName(nm); list.add(fileBean); } return list; } return null; }
public Vector<LsEntry> lsEntry(String path) { Vector<LsEntry> vct = new Vector<LsEntry>(); try { // ChannelSftp.LsEntry lsEntry = (LsEntry) v.get(i); Vector v = sftp.ls(path); for (Object obj : v) { LsEntry entry = (LsEntry) obj; if(!".".equals(entry.getFilename()) && !"..".equals(entry.getFilename())){ vct.add(entry); } } } catch (Exception e) { System.out.println(e.getMessage()); return new Vector<LsEntry>(); } return vct; }
@Bean public IntegrationFlow ftpInboundFlow(SftpSinkProperties properties, SessionFactory<LsEntry> ftpSessionFactory) { SftpMessageHandlerSpec handlerSpec = Sftp.outboundAdapter(new SftpRemoteFileTemplate(ftpSessionFactory), properties.getMode()) .remoteDirectory(properties.getRemoteDir()) .remoteFileSeparator(properties.getRemoteFileSeparator()) .autoCreateDirectory(properties.isAutoCreateDir()) .temporaryFileSuffix(properties.getTmpFileSuffix()); if (properties.getFilenameExpression() != null) { handlerSpec.fileNameExpression(properties.getFilenameExpression().getExpressionString()); } return IntegrationFlows.from(Sink.INPUT) .handle(handlerSpec, new Consumer<GenericEndpointSpec<FileTransferringMessageHandler<LsEntry>>>() { @Override public void accept(GenericEndpointSpec<FileTransferringMessageHandler<LsEntry>> e) { e.autoStartup(false); } }) .get(); }
/** * Delete directory and its content recursively. * * @param channel * open channel to SFTP server * @param path * path of the directory * @throws SftpException * when something went wrong */ @SuppressWarnings("unchecked") private void traverse(ChannelSftp channel, String path) throws SftpException { SftpATTRS attrs = channel.stat(path); if (attrs.isDir()) { Vector<LsEntry> files = channel.ls(path); if (files != null && files.size() > 0) { Iterator<LsEntry> it = files.iterator(); while (it.hasNext()) { LsEntry entry = it.next(); if ((!entry.getFilename().equals(".")) && (!entry.getFilename().equals(".."))) { traverse(channel, path + "/" + entry.getFilename()); } } } channel.rmdir(path); } else { channel.rm(path); } }
@Override @SuppressWarnings("unchecked") public Collection<File> listDirectory(String path) throws IOException { try { Vector<LsEntry> files = channel.ls(path); List<File> result = new ArrayList<File>(); if (files != null && files.size() > 0) { Iterator<LsEntry> it = files.iterator(); while (it.hasNext()) { LsEntry entry = it.next(); result.add(new File(path + "/" + entry.getFilename())); } } return result; } catch (SftpException e) { if (e.id == 2) { return Collections.emptyList(); } logger.error("listing the directory failed.", e); throw new IOException(e); } }
@Override @SuppressWarnings("unchecked") public List<String> ls(String path) throws FileBasedHelperException { try { List<String> list = new ArrayList<String>(); 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); } }
public static List<String> ls(String hostname, int port, String username, File keyFile, final String passphrase, String path) throws JSchException, IOException, SftpException { Session session = createSession(hostname, port, username, keyFile, passphrase); session.connect(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); @SuppressWarnings("unchecked") Vector<LsEntry> vector = (Vector<LsEntry>) channel.ls(path); channel.disconnect(); session.disconnect(); List<String> files = new ArrayList<String>(); for (LsEntry lse : vector) { files.add(lse.getFilename()); } return files; }
private static void recursiveDelete(ChannelSftp sftp, String path) throws SftpException, JSchException { Vector<?> entries = sftp.ls(path); for (Object object : entries) { LsEntry entry = (LsEntry) object; if (entry.getFilename().equals(".") || entry.getFilename().equals("..")) { continue; } if (entry.getAttrs().isDir()) { recursiveDelete(sftp, path + entry.getFilename() + "/"); } else { sftp.rm(path + entry.getFilename()); } } sftp.rmdir(path); }
/** * Filters the "." and ".." directories */ private boolean acceptListedFile(LsEntry file) { if (file == null || file.getFilename() == null) { return false; } String name = file.getFilename().trim().toLowerCase(); return ( ! ( name.endsWith("/..") || name.endsWith("\\..") || name.endsWith("/.") || name.endsWith("\\.") || name.equals("..") || name.equals(".") ) ); }
/** * This method is similar to getResource, except that the returned resource is fully initialized * (resolved in the sftp repository), and that the given string is a full remote path * * @param path * the full remote path in the repository of the resource * @return a fully initialized resource, able to answer to all its methods without needing any * further connection */ @SuppressWarnings("unchecked") public Resource resolveResource(String path) { try { List<LsEntry> r = getSftpChannel(path).ls(getPath(path)); if (r != null) { SftpATTRS attrs = r.get(0).getAttrs(); return new BasicResource(path, true, attrs.getSize(), attrs.getMTime() * MILLIS_PER_SECOND, false); } } catch (Exception e) { Message.debug("Error while resolving resource " + path, e); // silent fail, return nonexistent resource } return new BasicResource(path, false, 0, 0, false); }
@SuppressWarnings("unchecked") public List<String> list(String parent) throws IOException { try { ChannelSftp c = getSftpChannel(parent); String path = getPath(parent); Collection<LsEntry> r = c.ls(path); if (r != null) { if (!path.endsWith("/")) { path = parent + "/"; } List<String> result = new ArrayList<>(); for (LsEntry entry : r) { if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) { continue; } result.add(path + entry.getFilename()); } return result; } } catch (SftpException | URISyntaxException e) { throw new IOException("Failed to return a listing for '" + parent + "'", e); } return null; }
@Override public FileInfo listFile(String relativePath, boolean closeSession) { ChannelSftp sftp = null; FileInfo fileInfo = null; try { // Get a reusable channel if the session is not auto closed. sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_1); sftp.cd(basePath); if (!relativePath.equals(".") && !relativePath.equals("..")) { @SuppressWarnings("rawtypes") Vector list = sftp.ls(relativePath); for (Object object : list) { LsEntry entry = (LsEntry)object; if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) { long updateTime = entry.getAttrs().getMTime(); fileInfo = new FileInfo(relativePath, entry.getAttrs().isDir(), updateTime * 1000, entry.getAttrs().getSize()); } } } return fileInfo; } catch (Exception e) { return null; } finally { if (closeSession) { close(); } } }
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; }
@Override public Map<String, BlobMetaData> listBlobsByPrefix( String blobNamePrefix) throws IOException { try { final Vector<LsEntry> entries = blobStore.getClient().ls(path()); if (entries.isEmpty()) { return new HashMap<>(); } final String namePrefix = blobNamePrefix == null ? "" : blobNamePrefix; final MapBuilder<String, BlobMetaData> builder = MapBuilder .newMapBuilder(); for (final LsEntry entry : entries) { if (entry.getAttrs().isReg() && entry.getFilename().startsWith(namePrefix)) { builder.put(entry.getFilename(), new PlainBlobMetaData( entry.getFilename(), entry.getAttrs().getSize())); } } return builder.immutableMap(); } catch (Exception e) { throw new IOException("Failed to load files in " + path().buildAsString("/"), e); } }
public static void landFile(File fileToLand, UploadProperties properties) throws Exception { JSch jsch = new JSch(); String host = properties.getSftpServer(); String user = properties.getUser(); String password = properties.getPassword(); int port = properties.getPort(); Session session = jsch.getSession(user, host, port); session.setOutputStream(System.out); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(TIMEOUT); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp c = (ChannelSftp) channel; // delete any existing file with the target filename, so that rename will work @SuppressWarnings("unchecked") Vector<LsEntry> files = c.ls("."); for (LsEntry file : files) { if (FINAL_REMOTE_FILENAME.equals(file.getFilename())) { c.rm(FINAL_REMOTE_FILENAME); } } // transmit file, using temp remote name, so ingestion won't process file until complete c.put(new FileInputStream(fileToLand), TEMP_REMOTE_FILENAME, ChannelSftp.OVERWRITE); // rename remote file so ingestion can begin c.rename(TEMP_REMOTE_FILENAME, FINAL_REMOTE_FILENAME); c.disconnect(); channel.disconnect(); session.disconnect(); }
private List<FileEntry> listFiles(String path, ChannelSftp c) throws Exception { try { List<FileEntry> res = new ArrayList<FileEntry>(); @SuppressWarnings("rawtypes") java.util.Vector vv = c.ls(extractSessionPath(path)); if (vv != null) { for (int ii = 0; ii < vv.size(); ii++) { Object obj = vv.elementAt(ii); if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) { LsEntry lsEntry = (com.jcraft.jsch.ChannelSftp.LsEntry) obj; if ((lsEntry.getFilename().equals(".")) ||(lsEntry.getFilename().equals("..")) ) continue; FileEntry fileEntry = new FileEntry(); fileEntry.displayName = lsEntry.getFilename(); fileEntry.path = createFilePath(path, fileEntry.displayName); SftpATTRS attrs = lsEntry.getAttrs(); setFromAttrs(fileEntry, attrs); res.add(fileEntry); } } } return res; } catch (Exception e) { tryDisconnect(c); throw convertException(e); } }
/** * * @param directoryPath directory path * @return <code>true</code> if the directory is empty */ public boolean isDirectoryEmpty( String directoryPath ) { try { List<LsEntry> entries = new ArrayList<LsEntry>(); channel.ls(directoryPath, new LsEntrySelector(entries)); return entries.size() == 0; } catch (Exception e) { throw new JschSftpClientException(e.getMessage(), e); } }
/** * * @param directoryPath directory path * @return {@link List} of {@link FileEntry} objects corresponding with the target directory * file and folder entries. */ public List<FileEntry> ls( String directoryPath ) { try { List<LsEntry> entries = new ArrayList<LsEntry>(); channel.ls(directoryPath, new LsEntrySelector(entries)); return getFileEntries(entries, directoryPath); } catch (Exception e) { throw new JschSftpClientException(e.getMessage(), e); } }
private List<FileEntry> getFileEntries( List<LsEntry> lsEntries, String parentPath ) { List<FileEntry> fileEntries = new ArrayList<FileEntry>(); for (LsEntry lsEntry : lsEntries) { fileEntries.add(getFileEntry(lsEntry, parentPath)); } return fileEntries; }
private FileEntry getFileEntry( LsEntry lsEntry, String parentPath ) { FileEntry fileEntry = new FileEntry(lsEntry.getFilename(), IoUtils.normalizeUnixDir(parentPath) + lsEntry.getFilename(), lsEntry.getAttrs().isDir()); fileEntry.setSize(lsEntry.getAttrs().getSize()); fileEntry.setParentPath(parentPath); fileEntry.setLastModificationTime(lsEntry.getAttrs().getMTime()); return fileEntry; }
@Override public int select( LsEntry lsEntry ) { if (!lsEntry.getFilename().endsWith(".")) { this.entries.add(lsEntry); } return ChannelSftp.LsEntrySelector.CONTINUE; }
@Override public boolean exists(String srcPath) throws IOException { Vector<LsEntry> ls = null; try { ls = getSession().ls(getAbsolutePath(srcPath)); } catch (SftpException e){ return false; } return (ls.size()>0) ; }
/** * Issue an 'ls' command on remote server and exclussively print the values or return them in a String array. * * @param folder * to issue the 'ls' command on * @param printInfo * @return * @throws SftpException */ public String[] ls(String folder, boolean printInfo) throws SftpException { List fileList = cmd.ls(folder); String[] filenames = null; if (fileList != null) { //only instantiate array to hold ls results if user is not printing info if (!printInfo){ filenames = new String[fileList.size()]; } logger.debug("ls " + folder); int i = 0; for (Object obj : fileList) { if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) { LsEntry lsEntry = (LsEntry) obj; //either print or store each element if (printInfo) { logger.debug(lsEntry.getFilename()); } else { String fn = lsEntry.getFilename(); //filename if (fn != null && !fn.equals(".") && !fn.equals("..")) { filenames[i++] = fn; } } } } } return filenames; }
private JFTPFile toFtpFile(LsEntry lsEntry, String filePath) throws SftpException { String name = lsEntry.getFilename(); long fileSize = lsEntry.getAttrs().getSize(); String fullPath = String.format("%s%s%s", filePath, "/", lsEntry.getFilename()); // String fullPath = channel.realpath(filePath); //为何不用这个 int mTime = lsEntry.getAttrs().getMTime(); boolean directory = lsEntry.getAttrs().isDir(); return new JFTPFile(name, fileSize, fullPath, (long) mTime * MILLIS, directory); }
/** * 删除文件或文件夹 * @param fileName 当前目录的文件或文件夹的名称 */ public boolean remove(String fileName) throws Exception{ if (!isConnected()) { return false; } if(!isDir(fileName)){ sftp.rm(fileName);//如果是文件 直接删除 System.out.println("remove file: " + fileName + "."); }else{ System.out.println(fileName + " is a directory."); Vector<LsEntry> v = lsEntry(fileName); sftp.cd(fileName); System.out.println("enter dir " + fileName + "."); for (LsEntry file : v) { if(!isDir(file.getFilename())) { sftp.rm(file.getFilename()); System.out.println("remove file: " + file.getFilename() + "."); } else { remove(file.getFilename()); } } sftp.cd(".."); sftp.rmdir(fileName); System.out.println("remove empty dir: " + fileName + "."); } return true; }
public void ls(String path) { try { Vector v = sftp.ls(path); for(int i=0;i<v.size();i++){ LsEntry entry = (LsEntry) v.get(i); if (!".".equals(entry.getFilename()) && !"..".equals(entry.getFilename())) { System.out.println(entry.getLongname()); } } } catch (Exception e) { System.out.println(e.getMessage()); } }
@Bean public IntegrationFlow sftpInboundFlow(SessionFactory<LsEntry> sftpSessionFactory, SftpSourceProperties properties, FileConsumerProperties fileConsumerProperties) { SftpInboundChannelAdapterSpec messageSourceBuilder = Sftp.inboundAdapter(sftpSessionFactory) .preserveTimestamp(properties.isPreserveTimestamp()) .remoteDirectory(properties.getRemoteDir()) .remoteFileSeparator(properties.getRemoteFileSeparator()) .localDirectory(properties.getLocalDir()) .autoCreateLocalDirectory(properties.isAutoCreateLocalDir()) .temporaryFileSuffix(properties.getTmpFileSuffix()) .deleteRemoteFiles(properties.isDeleteRemoteFiles()); if (StringUtils.hasText(properties.getFilenamePattern())) { messageSourceBuilder.filter(new SftpSimplePatternFileListFilter(properties.getFilenamePattern())); } else if (properties.getFilenameRegex() != null) { messageSourceBuilder .filter(new SftpRegexPatternFileListFilter(properties.getFilenameRegex())); } IntegrationFlowBuilder flowBuilder = IntegrationFlows.from(messageSourceBuilder , new Consumer<SourcePollingChannelAdapterSpec>() { @Override public void accept(SourcePollingChannelAdapterSpec sourcePollingChannelAdapterSpec) { sourcePollingChannelAdapterSpec .poller(SftpSourceConfiguration.this.defaultPoller); } }); return FileUtils.enhanceFlowForReadingMode(flowBuilder, fileConsumerProperties) .channel(this.source.output()) .get(); }
@Bean public SessionFactory<LsEntry> sftpSessionFactory() { @SuppressWarnings("unchecked") SessionFactory<LsEntry> ftpSessionFactory = Mockito.mock(SessionFactory.class); @SuppressWarnings("unchecked") Session<LsEntry> session = mock(Session.class); when(ftpSessionFactory.getSession()).thenReturn(session); return ftpSessionFactory; }
private FTPFile toFtpFile(LsEntry lsEntry, String filePath) throws SftpException { String name = lsEntry.getFilename(); long fileSize = lsEntry.getAttrs().getSize(); int mTime = lsEntry.getAttrs().getMTime(); boolean directory = lsEntry.getAttrs().isDir(); return new FTPFile(name, fileSize, filePath, (long) mTime * 1000, directory); }