如何解决如何使用 Java 从 unix 目录中提取文件并将它们传输到 aure Blob 存储或 S3 存储桶
我有一个 unix 目录,我必须从中获取特定文件,然后我必须将它们放入 s3 存储桶或放入 azure blob 存储中。我已经写了相同的代码
public class JSchExampleSSHConnection {
public static void main(String[] args) {
String host="my.host.com";
String user="iamUser";
String password="iampass";
String watchFolder ="//usd/sync$/TestRepo/TestFiles/"; // folder from where i want to pull the files & this this path exactly the same of the unix directory
String filemask = "*.csv";
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
Integer port = 22;
try{
session = jsch.getSession(user,host,port);
session.setConfig("StrictHostKeyChecking","no");
session.setPassword(password);
session.connect();
channel = session.openChannel("sftp");
channel.connect(); //connected here
ChannelSftp sftpChannel = (ChannelSftp)channel;
// Go through watch folder looking for files.
File[] files = findFile(watchFolder,filemask);
for(File file : files) {
// Upload file. // i can call a function from here which can push the files into s3/blob storage
System.out.print(file);
}
}catch(Exception e){
e.printStackTrace();
}
}
public static File[] findFile(String dirName,final String mask) {
File dir = new File(dirName);
return dir.listFiles(new FilenameFilter() {
public boolean accept(File dir,String filename)
{ return filename.endsWith(mask); }
} );
}
}
但是我的代码的问题是它没有将文件返回给我。任何人都可以帮助我在这里做错了什么。
解决方法
String filemask = "*.csv";
...
public boolean accept(File dir,String filename)
{ return filename.endsWith(mask); }
}
我猜您想查找以“.csv”结尾的文件名?您的 filemask
看起来应该是 wildcard expression,因为它以星号开头。
但是,您将 filemask
传递给 [String.endsWith()][2]
。此方法不进行通配符匹配。它将检查以文字“*.csv”结尾的文件名,包括星号。那只会匹配包含文字星号的文件名。
如果您想查找以“.csv”结尾的文件名,请将您的文件掩码设为“.csv”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。