如何使用 JSch 的 SFTP 通道只获取目录和文本文件?
How to get only directories and text files using SFTP channel of JSch?
您好,我正在开发一个连接到远程服务器并浏览不同目录的应用程序。
这里我只想向用户显示目录和文本文件。在 JSch 中使用 SFTP 通道,我可以执行 ls
方法。但是这种方法可以给我这种格式 "*"
或 "*.txt"
的结果。分别使用 ls
我可以获得目录列表和文本文件列表。由于我单独使用它,因此我必须使用 2 种不同的 ls
方法,例如:
sftpChannel.ls("*");
sftpChannel.ls("*.txt");
1st 为我提供了我必须从中循环和过滤目录的所有条目。其次,我得到了所有文本文件。
如何使用最少的代码获取目录列表和文本文件列表。我不想循环两次。谢谢
使用ls("")
。然后循环返回的条目,select只有你想要的。
即LsEntry.getFilename()
以 ".txt"
或 LsEntry.getAttrs().isDir()
.
结尾的那些
We can use like this, read directories and files also.
public List<String> readRemoteDirectory(String location){
System.out.println("Reading location : "+location);
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> filesList = new ArrayList<String>();
String separator = getSeparator();
try{
JSch jsch = new JSch();
session = jsch.getSession(remote_server_user,remote_server_ip,22);
session.setPassword(remote_server_password);
java.util.Properties config = new java.util.Properties();
config.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(location);
Vector filelist = channelSftp.ls("*");
for(int i=0; i<filelist.size();i++){
LsEntry entry = (LsEntry) filelist.get(i);
if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) {
continue;
}
if(entry.getAttrs().isDir()){
System.out.println(entry.getFilename());
//System.out.println("Entry"+location+separator+entry.getAttrs());
filesList.add(entry.getFilename());
}
}
}catch(Exception ex){
ex.printStackTrace();
logger.debug(ex.getMessage());
if(ex.getMessage().equals("No such file")){
logger.debug("No Such File IF");
}
}finally{
channel.disconnect();
session.disconnect();
}
return filesList;
}
您好,我正在开发一个连接到远程服务器并浏览不同目录的应用程序。
这里我只想向用户显示目录和文本文件。在 JSch 中使用 SFTP 通道,我可以执行 ls
方法。但是这种方法可以给我这种格式 "*"
或 "*.txt"
的结果。分别使用 ls
我可以获得目录列表和文本文件列表。由于我单独使用它,因此我必须使用 2 种不同的 ls
方法,例如:
sftpChannel.ls("*");
sftpChannel.ls("*.txt");
1st 为我提供了我必须从中循环和过滤目录的所有条目。其次,我得到了所有文本文件。
如何使用最少的代码获取目录列表和文本文件列表。我不想循环两次。谢谢
使用ls("")
。然后循环返回的条目,select只有你想要的。
即LsEntry.getFilename()
以 ".txt"
或 LsEntry.getAttrs().isDir()
.
We can use like this, read directories and files also.
public List<String> readRemoteDirectory(String location){
System.out.println("Reading location : "+location);
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> filesList = new ArrayList<String>();
String separator = getSeparator();
try{
JSch jsch = new JSch();
session = jsch.getSession(remote_server_user,remote_server_ip,22);
session.setPassword(remote_server_password);
java.util.Properties config = new java.util.Properties();
config.put("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
channelSftp.cd(location);
Vector filelist = channelSftp.ls("*");
for(int i=0; i<filelist.size();i++){
LsEntry entry = (LsEntry) filelist.get(i);
if (".".equals(entry.getFilename()) || "..".equals(entry.getFilename())) {
continue;
}
if(entry.getAttrs().isDir()){
System.out.println(entry.getFilename());
//System.out.println("Entry"+location+separator+entry.getAttrs());
filesList.add(entry.getFilename());
}
}
}catch(Exception ex){
ex.printStackTrace();
logger.debug(ex.getMessage());
if(ex.getMessage().equals("No such file")){
logger.debug("No Such File IF");
}
}finally{
channel.disconnect();
session.disconnect();
}
return filesList;
}