Java 无法在ubuntu 18.04中使用apache commons vfs for vsftpd重命名文件
我正在尝试使用apache commons vfs重命名vsftpd服务器中的文件,moveTo函数在本地系统操作系统(Kubuntu 19.04)和vsftpd服务器中运行良好,但当我尝试在具有ubuntu 18.04的测试环境中重命名该文件时,我无法重命名该文件,我遇到异常 使用此代码:Java 无法在ubuntu 18.04中使用apache commons vfs for vsftpd重命名文件,java,sftp,ubuntu-18.04,apache-commons-vfs,vsftpd,Java,Sftp,Ubuntu 18.04,Apache Commons Vfs,Vsftpd,我正在尝试使用apache commons vfs重命名vsftpd服务器中的文件,moveTo函数在本地系统操作系统(Kubuntu 19.04)和vsftpd服务器中运行良好,但当我尝试在具有ubuntu 18.04的测试环境中重命名该文件时,我无法重命名该文件,我遇到异常 使用此代码: public static boolean move(String hostName, String username, String password, String remoteSrcFileP
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;
}
}
我有一个例外
org.apache.commons.vfs2.FileSystemException: Could not determine if file "sftp://ftpuser:***@ip_address/home/ftpuser/ftp/1/Documents/test1/test2" is writeable
请注意,上述代码在本地运行得非常好。如果您遵循以下步骤,您将发现:
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());
}
}
if(canRenameTo(destFile)){//src和dest具有相同的FS
如果(!getParent().isWriteable()){/@diginoise,我尝试了使用apache commons vfs,但没有成功,只有在重命名时,我才切换到下面的Jsch,这是本地和远程服务器的工作代码
private static ChannelSftp setupJsch(String hostName, String username, String password) throws JSchException {
JSch jsch = new JSch();
JSch.setConfig("StrictHostKeyChecking", "no");
jsch.setKnownHosts("/home/"+username+"/.ssh/known_hosts");
Session jschSession = jsch.getSession(username, hostName);
jschSession.setPassword(password);
jschSession.connect();
return (ChannelSftp) jschSession.openChannel("sftp");
}
public static boolean renameFile(String hostName, String username, String password, String remoteSrcFilePath,
String remoteDestFilePath) {
boolean result = false;
ChannelSftp channelSftp = null;
try {
channelSftp = setupJsch(hostName,username,password);
channelSftp.connect();
channelSftp.rename(remoteSrcFilePath, remoteDestFilePath);
result = true;
}catch(JSchException | SftpException e) {
logger.error("Error while renaming file sftp ",e);
}finally {
sftpCleanUp(channelSftp);
}
return result;
}
private static void sftpCleanUp(ChannelSftp channelSftp) {
if(null != channelSftp) {
channelSftp.disconnect();
channelSftp.exit();
}
}
我有一个问题,因为我无法复制您的代码。在SFTP服务器上远程操作文件之前,Apache Commons VFSi将检查父目录的权限,您有权使用服务器上的命令?@vincenzopalazzo用户有权访问,因为该文件是由同一用户创建的。该文件是由同一用户创建的,VPS服务器中的SSH阻塞还是Ubuntu阻塞要重命名为ftpuser可以创建和删除file@pise您是否尝试通过sftp
从命令行或任何其他sftp客户端移动该文件?我尝试使用filezilla重命名该文件,结果成功。