我需要使用jsch将我的Java代码连接到sftp服务器,并且这部分没问题。
我的问题是,我想从服务器中的路径获取文件并删除所有文件。
我可以获取文件,但是不能删除。
我正在阅读有关此问题的信息,并且错误与权限有关。
这里是具有权限的filezilla图像。
这是我的方法之一:
public void changePermissions(int permissions,String path)
{
int Octal = Integer.parseInt(Integer.toString(permissions), 8);
Try<Session> of = Try.of(() -> {
JSch jsch = new JSch();
this.session = jsch.getSession("myuser", "xxx.xxx.xxx.2 ", 22);
this.session.setPassword("mypassword1");
this.session.setConfig("StrictHostKeyChecking", "no");
this.session.connect();
String comando = "sudo chmod 777 -R "+path;
Channel canal = this.session.openChannel("exec");
System.out.println("OEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");
InputStream in = canal.getInputStream();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
canal.setOutputStream(baos);
((ChannelExec) canal).setCommand("cd "+path);
((ChannelExec) canal).setCommand("pwd");
((ChannelExec) canal).setCommand("sudo -S -p '' "+comando);
String s = new String(baos.toByteArray());
System.out.println(s);
return this.session;
});
}
这是我的其他Java代码
public void changePermissions(String path){
Try<ChannelSftp> tryConection = Try.of(() -> {
ChannelSftp channelSftp = (ChannelSftp) this.session
.openChannel("sftp");
channelSftp.connect();
return channelSftp;
});
Try<Boolean> of1 = Try.of(() -> {
tryConection.get().chmod(511,path);
return true;
}
Try<Boolean> of = Try.of(() -> {
tryConection.getOrElseThrow(e -> new Error("failed :( " + e.getMessage())).rmdir(path);
return true;
});
}
我用其他在执行过程中正确的方法获得的会话对象。
第一个选项不显示任何内容,但是第二个选项显示在日志“ 3:权限被拒绝”中