尝试使用JSch API重命名文件/文件夹,因为Shell命令无法执行它们。 mkdir和rmdir运行正常。
JDK版本-11.
JSch版本-0.1.55。
任何线索都将不胜感激。预先感谢。
使用以下代码。
ChannelExec execChannel = null;
try {
execChannel = (ChannelExec) session.openChannel("exec");
execChannel.connect();
execChannel.setCommand("rename dir1 dir2");
execChannel.start();
} catch (JSchException ex) {
throw new IOException(ex);
} finally {
if (execChannel != null) {
execChannel.disconnect();
}
}
更新:
答案 0 :(得分:0)
如果您使用的是Jsch,我相信远程主机是一个类似Unix的系统。在Unix中没有命令rename
。请改用mv
。
答案 1 :(得分:0)
您很可能执行了错误的命令。如果要连接到Linux,则要使用的命令是mv
。
尽管如此,您不应使用shell命令来执行琐碎的文件操作,例如创建和删除目录以及重命名文件/目录。这是一种非常脆弱的方法。
请使用SSH的标准文件管理API,SFTP(在JSch中为ChannelSftp
)。
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
sftpChannel.mkdir("/path/dir1")
sftpChannel.rename("/path/dir1", "/path/dir2");
sftpChannel.rmdir("/path/dir2");