i need recursive list of file names in sftp folder. i'm using following code:
private void getfilesrecursive(list<lsentry> filelist, list<filedto> response, string dirparentname, sftpmanagerwithpool manager) throws sftpexception { (lsentry file : filelist) { if (!file.getattrs().isdir()) { response.add(new filedto(file.getfilename(), storagetype.folder.getname(), new attributes(file.getattrs().getatime(), file.getattrs().getmtime(), file.getattrs().getatimestring(), file.getattrs().getmtimestring(), file.getattrs().getpermissionsstring(), getpathwitoutrootdirectoryt(dirparentname), file.getattrs().getsize(), file.getattrs().isdir()))); } else if (file.getattrs().isdir() && !".".equals(file.getfilename())) { list<lsentry> files = manager.listfiles(context.getbasepath().concat("/") .concat(dirparentname.concat("/").concat(file.getfilename()))); getfilesrecursive(files, response, dirparentname.concat("/").concat(file.getfilename()), manager); } } } public list<filedto> getfiles() throws serviceexception { session session = null; sftpmanagerwithpool manager = null; try { session = getsession(); manager = new sftpmanagerwithpool(session); manager.connect(); list<filedto> response = new arraylist<>(); list<lsentry> files = manager .listfiles(context.getbasepath().concat("/").concat(context.getpathtoprocess())); getfilesrecursive(files, response, context.getpathtoprocess(), manager); return response; } catch (exception e) { throw new serviceexception(httpstatus.internal_server_error.value(), e.getmessage(), e.getlocalizedmessage()); } { if (manager != null) { manager.disconnect(); try { returnsession(session); } catch (exception e) { throw new serviceexception(httpstatus.internal_server_error.value(), e.getmessage(), e.getlocalizedmessage()); } } } }
the following method in sftpmanagerwithpool :
@suppresswarnings("unchecked") public list<lsentry> listfiles(string pathfrom) throws sftpexception { if (c == null || session == null || !session.isconnected() || !c.isconnected()) { throw new sftpexception(errorcode.list_files.ordinal(), "connection server closed. open first."); } string filepath = pathfrom + "/"; return c.ls(filepath); }
everything works no more 5k files when have more files takes lot of time , results in timeouts.
my question is, how can use c.ls(filepath);
method list first n files in folder? i'm looking similar linux shell command ls -u | head -4
----edit-----
i modified method listfiles
follow still don't better performance:
public list<lsentry> listfiles(string pathfrom, int top) throws sftpexception { if (c == null || session == null || !session.isconnected() || !c.isconnected()) { throw new sftpexception(errorcode.list_files.ordinal(), "connection server closed. open first."); } string filepath = pathfrom + "/"; list<lsentry> response = new arraylist<>(); c.ls(filepath, new lsentryselector() { @override public int select(lsentry record) { if (response.size() <= top) { response.add(record); return lsentryselector.continue; } else { return lsentryselector.break; } } }); return response; }
thank :)
use overload of channelsftp.ls
method takes lsentryselector
interface:
public void ls(string path, lsentryselector selector)
implement lsentryselector.select
collect file entries. once have enough of them, make return break
.
Comments
Post a Comment