java sftp.exec无法执行mv命令

编写java程序过程中,sftp上传下载建目录删除文件都可以,就是备份不行。

分析原因如下:

1.如果用的同一个用户,即sftp用户来通过 exec(ssh连接) 执行mv命令,那极有可能是在搭建sftp服务时,该用户被限制只能sftp禁用ssh,解决可用:查看这里

2.排除上一个原因后,那我们就只能调试该命令的返回结果

java代码

public void exec(Session session,String command) {
        ChannelExec channelExec = null;
        try {
            System.out.println("Session connected.");
            System.out.println("Opening Channel.");
            Channel channel = session.openChannel("exec");
            channelExec = (ChannelExec) channel;
            channelExec.setCommand(command);
            channelExec.connect();
        //加入以下代码,将ssh命令登陆的结果返回到控制台 channelExec.setInputStream(
null); InputStream in = channelExec.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String buf = null; while ((buf = reader.readLine()) != null) { System.out.println(buf); } reader.close(); } catch (Exception e) { e.printStackTrace(); channelExec = null; }finally { channelExec.disconnect(); } }

控制台输出 

This service allows sftp connections only.

这里输出了This service allows sftp connections only.即第一种情况

搭建sftp完整资料参考:https://www.cnblogs.com/erdi/p/9988136.html

springboot sftp项目参照:https://blog.csdn.net/qq_37293819/article/details/80670515

猜你喜欢

转载自www.cnblogs.com/pu20065226/p/10962977.html