Java 使用Apache Commons VFS2检查文件是否存在
我想问一下,是否有办法只使用Apache Commons检查文件夹中是否已经存在文件 我有一个上传到SFTP文件夹的方法,但它会在该方法运行时覆盖当前文件。该方法设置为每5分钟运行一次。我需要一个代码来创建和if语句,该语句检查文件是否已经不在SFTP位置,然后,if not执行我的copy方法,如果有文件,则跳过它 我的复制方法如下所示Java 使用Apache Commons VFS2检查文件是否存在,java,file,sftp,apache-commons-vfs,Java,File,Sftp,Apache Commons Vfs,我想问一下,是否有办法只使用Apache Commons检查文件夹中是否已经存在文件 我有一个上传到SFTP文件夹的方法,但它会在该方法运行时覆盖当前文件。该方法设置为每5分钟运行一次。我需要一个代码来创建和if语句,该语句检查文件是否已经不在SFTP位置,然后,if not执行我的copy方法,如果有文件,则跳过它 我的复制方法如下所示 private void copyFileSFTP(File model, String hour) throws IOException { Sta
private void copyFileSFTP(File model, String hour) throws IOException {
StandardFileSystemManager manager = new StandardFileSystemManager();
String dest = String.format("%s/%s/model/%s", destinationPath, hour,
model.getName());
remoteDirectory = String.format("%s/%s/model/", destinationPath, hour);
try {
if (!model.exists())
LOG.error("Error. Local file not found");
// Initializes the file manager
manager.init();
// Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts,
false);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
// Create the SFTP URI using the host name, userid, password, remote
// path and file name
String sftpUri = "sftp://" + userId + ":" + password + "@"
+ serverAddress + "/" + remoteDirectory + model.getName();
**HERE I NEED THE CHECK IF THE MODEL EXISTS ALREADY ON SFTP**
// Create local file object
FileObject localFile = manager.resolveFile(model.getAbsolutePath());
// Create remote file object
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
LOG.info("File upload successful");
LOG.info("New file has been created.");
LOG.info(dest);
} catch (Exception ex) {
LOG.error(ex);
handleBadPath(model, hour);
} finally {
manager.close();
}
}
谢谢您的帮助。使用
FileObject.exists()
方法
请参阅使用
FileObject.exists()
方法
请参阅使用
FileObject.exists()
方法
请参阅使用
FileObject.exists()
方法
请参见我已检查,这看起来像是先创建文件,然后检查(编号4)-未检查,然后创建。我已检查,这看起来像是先创建文件,然后检查(编号4)-未检查,然后创建。我已检查,这看起来像是先创建文件,然后检查(编号4)-未检查,然后创建。我已检查,这看起来像是先创建文件,然后检查(编号4)-未检查,然后创建。那么第四部分最终会起作用吗?对不起,我的意思是第二个链接:检查文件是否存在于远程主机位置是的,但他们根本不使用返回值
.exists()
。他们使用该检查可能是为了验证上传成功,而不是为了防止覆盖。所以第四部分最终会起作用吗?对不起,我的意思是第二个链接:检查文件是否存在于远程主机位置是的,但他们根本不使用返回值.exists()
。他们使用该检查可能是为了验证上传成功,而不是为了防止覆盖。所以第四部分最终会起作用吗?对不起,我的意思是第二个链接:检查文件是否存在于远程主机位置是的,但他们根本不使用返回值.exists()
。他们使用该检查可能是为了验证上传成功,而不是为了防止覆盖。所以第四部分最终会起作用吗?对不起,我的意思是第二个链接:检查文件是否存在于远程主机位置是,除非他们根本不使用返回值.exists()
。他们使用检查可能是为了验证上传成功,而不是为了防止覆盖。