Java SFTP/SSH目录不删除/删除该目录
我试着到处搜索,但到处都找不到。 我试图完成的是转到SFTP或SSH并删除/删除目录 这是我的密码。感谢您的帮助。此代码不会像预期的那样删除和删除目录Java SFTP/SSH目录不删除/删除该目录,java,ssh,jsch,Java,Ssh,Jsch,我试着到处搜索,但到处都找不到。 我试图完成的是转到SFTP或SSH并删除/删除目录 这是我的密码。感谢您的帮助。此代码不会像预期的那样删除和删除目录 public static boolean removeDirectory(String path, Session session) throws InterruptedException { ChannelExec channelExec = null; try { channelEx
public static boolean removeDirectory(String path, Session session) throws InterruptedException {
ChannelExec channelExec = null;
try {
channelExec = (ChannelExec) session.openChannel("exec");
String command = "rm -rf "+path;
channelExec.setCommand(command);
channelExec.connect();
Thread.sleep(5000);
channelExec.disconnect();
} catch (JSchException e1) {
return false;
}
return true;
}
我终于找到了解决办法。我已经使用jScape库递归地删除了目录
import java.util.List;
import org.apache.log4j.Logger;
import com.jscape.inet.sftp.Sftp;
import com.jscape.inet.sftp.SftpException;
import com.jscape.inet.sftp.events.SftpAdapter;
import com.jscape.inet.ssh.util.SshParameters;
public class SFTPExample extends SftpAdapter {
static String hostName = "hostname";
static String username = "username";
static String password = "password";;
static String directory = "directory";;
private static Sftp sftp;
private static org.apache.log4j.Logger log = Logger.getLogger(SFTPExample.class);
@SuppressWarnings("unchecked")
public static boolean deleteDir(List <String> path) throws SftpException {
log.info("------------------------ file(s) delete started ------------------------");
sftp = new Sftp(new SshParameters(hostName, username, password));
sftp.connect();
sftp.setDir(directory);
for (String eachOne : path) {
if (!sftp.getDirListingAsString(eachOne).equals("")){
log.info(" ------ File Name: " + eachOne);
System.out.println(directory+eachOne);
sftp.deleteDir(directory+eachOne, true);
}
}
sftp.disconnect();
log.info("------------------------ file(s) delete finished -----------------------");
return true;
}
// open connection to the remote server.
public static void openConnection() throws SftpException {
sftp.connect();
}
// disconnect from the remote server.
public static void closeConnection() {
sftp.disconnect();
}
}
远程目录中包含多少文件?是否有可能命令没有在您等待它的五秒钟内完成?运行此操作时是否出现异常?异常中的消息是什么?没有引发异常,当我尝试执行命令->rm-rf directory\u name时,它会在1秒内完成。