ChannelSftp.chmod方法賦權文件夾


最近對接分賬工作需要上傳分賬文件到指定sftp服務器,在給sftp上文件夾賦權是遇到一個問題;

具體是ChannelSftp的chmod(int permissions, String path)方法,該方法第一個參數作用是設定文件夾訪問權限,需要將 10進制 的權限數值轉為 8進制 寫入。

代碼: 

 1      public class SftpChmodDemo{
 2          
 3          public static void main(String[] args) throws ClassNotFoundException {
 4              String userName = "username";
 5              String password = "password";
 6              String ip = "localhost";
 7              int port = 22;
 8              String path = "/user";
 9      
10              Session session = null;
11              Channel channel = null;
12              try {
13                  session = new JSch().getSession(userName, ip, port);
14                  if (session == null) {
15                      throw new Exception("session is null");
16                  }
17                  session.setPassword(password);
18                  session.setConfig("StrictHostKeyChecking", "no");
19                  session.connect(3000000);
20                  channel = session.openChannel("sftp");
21                  channel.connect(3000000);
22                  ChannelSftp sftp = (ChannelSftp) channel;
23                  // 設定777權限,轉為8進制放入chmod中
24                  sftp.chmod(Integer.parseInt("777", 8), path);
25                  // sftp.chmod(Integer.parseInt("755", 8), path);
26                  // sftp.chmod(Integer.parseInt("775", 8), path);
27      
28              } catch (Exception e) {
29                  // TODO: handle exception
30              } finally {
31                  if (null != session) {
32                      session.disconnect();
33                  }
34                  if (null != channel) {
35                      channel.disconnect();
36                  }
37              }
38          }
39      }

 

然后查看文件夾屬性,賦權成功了!

 

 

 貼一張文件訪問權限圖,根據需要賦權:

 

 最后,感謝大家訪問,不正確的地方歡迎留言!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM