我正在尝试使用 apache commons vfs 重命名 vsftpd 服务器中的文件,moveTo 函数在本地系统操作系统(Kubuntu 19.04)和 VSFTPD 服务器中工作正常,但是当我尝试在具有 ubuntu 18.04 的测试环境中重命名该文件时我无法重命名出现异常的文件。
使用此代码:
public static boolean move(String hostName, String username, String password, String remoteSrcFilePath,
String remoteDestFilePath, byte [] data) {
FileObject remoteFile = null;
FileObject remoteDestFile = null;
boolean result = false;
try (StandardFileSystemManager manager = new StandardFileSystemManager()){
manager.init();
// Create remote object
remoteFile = manager.resolveFile(
createConnectionString(hostName, username, password, remoteSrcFilePath), createDefaultOptions());
remoteDestFile = manager.resolveFile(
createConnectionString(hostName, username, password, remoteDestFilePath), createDefaultOptions());
if (!remoteDestFile.exists() && remoteFile.exists()) {
remoteFile.moveTo(remoteDestFile);
if(null != data)
writeData(remoteDestFile, data);
result = true;
}else {
throw new DataIntegrityViolationException("Destination path already exists");
}
} catch (IOException e) {
logger.error("Error while renaming/moving file",e);
} finally {
try {
if(null != remoteDestFile)
remoteDestFile.close();
if(null != remoteFile)
remoteFile.close();
} catch (FileSystemException e) {
logger.warn("error while closing fileobject "+e.getMessage());
}
}
return result;
}
public static FileSystemOptions createDefaultOptions() throws FileSystemException {
// Create SFTP options
FileSystemOptions opts = new FileSystemOptions();
// SSH Key checking
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
/*
* Using the following line will cause VFS to choose File System's Root as VFS's
* root. If I wanted to use User's home as VFS's root then set 2nd method
* parameter to "true"
*/
// Root directory set to user home
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
// Timeout is count by Milliseconds
SftpFileSystemConfigBuilder.getInstance().setConnectTimeoutMillis(opts, 10000);
return opts;
}
}
我有这个异常(exception)
org.apache.commons.vfs2.FileSystemException: Could not determine if file "sftp://ftpuser:***@ip_address/home/ftpuser/ftp/1/Documents/test1/test2" is writeable
请注意,上面的代码在本地运行得很好。
最佳答案
如果您遵循source code for apache commons vfs for moveTo()你会发现:
if (canRenameTo(destFile)) { //src and dest have the same FS
if (!getParent().isWriteable()) { // <-- it could throw the same exception here
throw new FileSystemException("vfs.provider/rename-parent-read-only.error", getName(),
getParent().getName());
}
} else {
if (!isWriteable()) { // <---- it throws inside here (IMO) rather than returning false
throw new FileSystemException("vfs.provider/rename-read-only.error", getName());
}
}
...,您会发现,如果目标文件不可写,但以灾难性的方式(如 isWriteable( file)
在其主体内部抛出异常,而不是返回 false
。
我的第一个调用是验证 sftpd 进程 和 ftp 用户 是否可以写入您想要将文件移动到的目录。
呵呵!
关于java - 无法在 ubuntu 18.04 中使用 apache commons vfs for vsftpd 重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58246588/