Java 使用JSch库基于某些正则表达式在sftp位置搜索文件
我在sftp位置有3种文件,如下所示:Java 使用JSch库基于某些正则表达式在sftp位置搜索文件,java,regex,Java,Regex,我在sftp位置有3种文件,如下所示: String REGEX_PATTERN = "(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg"; int SFTPPORT = 22; Session session = null; Channel channel = null; ChannelS
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
UploadBusinessLicense\u xyzbus\uzbus.jpg
GovernmentPhotoID\u xyzgovern.jpg
CustomerConsentForm\u xyzconsent\uz.jpg
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
我尝试了以下模式
上传业务许可证*|政府照片ID*而且它不起作用。我是新加入regex的。所以任何帮助都将不胜感激
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
字符串正则表达式\u模式=
“(UploadBusinessLicense | CustomerConsentForm | UploadBusinessLicense)\.\\.jpg”;
int-SFTPPORT=22;
会话=空;
通道=空;
ChannelSftp ChannelSftp=null;
列表文件名=新的ArrayList();
向量列表=空;
JSch JSch=新的JSch();
试一试{
session=jsch.getSession(“用户名”,“主机”,22);
session.setPassword(“密码”);
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
session.setConfig(config);
session.connect();
通道=会话.openChannel(“sftp”);
channel.connect();
channelSftp=(channelSftp)信道;
channelSftp.cd(“文件目录”);
System.out.println(“获取文件”);
列表=channelSftp.ls(正则表达式模式);
对于(ChannelSftp.LsEntry条目:列表){
channelSftp.get(entry.getFilename(),“/home/tofankumarbal/Pictures”);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
if(channelSftp!=null&&channelSftp.isConnected()){
channelSftp.exit();
session.disconnect();
通道断开();
}
}捕获(例外e){
}
}
}
使用上述正则表达式搜索后,向量列表变为空。从问题中,我了解到您希望将3种字符串与以下条件匹配
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
String regex=“(UploadBusinessLicense[a-zA-z]+bus\GovernmentPhotoID[a-zA-z]+government\customerconcentform[a-zA-z]+同意)\ud+\.jpg” String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
注意:在Java中使用正则表达式时,请使用extra\
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
我使用的字符串示例如下:
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}
- 上传BusinessLicense_xyzbus_123452454.jpg
- 政府照片ID_xyzgovern_12345.jpg
- CustomerConsentForm_xyzconsent_5456456.jpg
我希望这能解决您的问题。提供示例输入/输出以及您尝试过的内容。谢谢您的建议。但我怀疑这也不起作用。但是当我用UploadBusinessLicense搜索时,它会给我所有UploadBusinessLicense文件。关于文件搜索模式,jsch库中是否有某种限制
String REGEX_PATTERN =
"(UploadBusinessLicense|CustomerConsentForm|UploadBusinessLicense)_.*?_.*?\\.jpg";
int SFTPPORT = 22;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
List<String> fileNames = new ArrayList<String>();
Vector<ChannelSftp.LsEntry> list = null;
JSch jsch = new JSch();
try {
session = jsch.getSession("username", "host", 22);
session.setPassword("password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("file directory");
System.out.println("fetching files");
list = channelSftp.ls(REGEX_PATTERN);
for (ChannelSftp.LsEntry entry : list) {
channelSftp.get(entry.getFilename(), "/home/tofankumarbal/Pictures");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (channelSftp != null && channelSftp.isConnected()) {
channelSftp.exit();
session.disconnect();
channel.disconnect();
}
} catch (Exception e) {
}
}
}