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