编写java程序过程当中,sftp上传下载建目录删除文件均可以,就是备份不行。html
分析缘由以下:java
1.若是用的同一个用户,即sftp用户来经过 exec(ssh链接) 执行mv命令,那极有多是在搭建sftp服务时,该用户被限制只能sftp禁用ssh,解决可用:查看这里。spring
2.排除上一个缘由后,那咱们就只能调试该命令的返回结果springboot
java代码session
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(); } }
控制台输出 ssh
This service allows sftp connections only.
这里输出了This service allows sftp connections only.即第一种状况spa
搭建sftp完整资料参考:http://www.javashuo.com/article/p-zfagghkm-my.html.net
springboot sftp项目参照:https://blog.csdn.net/qq_37293819/article/details/80670515调试