編寫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調試