2013-02-20 66 views

回答

0

如果您使用的是Java 7,则应该可以使用POSIX functionality来执行此操作。具体来说,getPosixFilePermissions方法应该是你要找的。有关更多信息,请参阅this question

+0

谢谢你的回答。但我想在java中使用这个getPosixFilePermissions方法6 – 2013-02-20 16:35:22

+0

它在Java 6中不可用。你可以升级到Java 7吗? – 808sound 2013-02-20 16:36:09

+0

因为我们的开发框架是基于Java 6的,所以在我们的项目中不能使用Java 7。 – 2013-02-20 16:40:18

0
package Test.Dir.Onlinux; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import ch.ethz.ssh2.Connection; 
import ch.ethz.ssh2.Session; 
import ch.ethz.ssh2.StreamGobbler; 
import java.lang.System; 

public class TestDirOnLinux { 


    public static void main(String[] args) { 

     String linuxCommands[] = { "stat -c %U ", "stat -c %G ", "stat -c %A ", "stat -c %a " }; 
     String fileName = "/home/zmt/UberSVN"; 
     excuteLinuxCommand("192.168.0.26", "root", "[email protected]", linuxCommands[0] + fileName); 
     excuteLinuxCommand("192.168.0.26", "root", "[email protected]", linuxCommands[1] + fileName); 
     excuteLinuxCommand("192.168.0.26", "root", "[email protected]", linuxCommands[2] + fileName); 
     excuteLinuxCommand("192.168.0.26", "root", "[email protected]", linuxCommands[3] + fileName); 
    } 

    public static void excuteLinuxCommand(String ipAddress, String userName, String password, String linuxCommands) { 

     boolean isAuthenticated = false; 

     try { 
      // Connection conn = new Connection(hostname); 
      Connection conn = new Connection(ipAddress); 
      conn.connect(); 
      // isAuthenticated = conn.authenticateWithPassword(username, 
      // password); 
      isAuthenticated = conn.authenticateWithPassword(userName, password); 

      if (isAuthenticated == false) 
       throw new IOException("Authentication failed."); 

      Session sess = conn.openSession(); 

      // sess.execCommand("shutdown -h now"); 
      sess.execCommand(linuxCommands); 

      InputStream stdout = new StreamGobbler(sess.getStdout()); 
      InputStream stderr = new StreamGobbler(sess.getStderr()); 

      InputStreamReader insrout = new InputStreamReader(stdout); 
      InputStreamReader insrerr = new InputStreamReader(stderr); 

      BufferedReader stdoutReader = new BufferedReader(insrout); 
      BufferedReader stderrReader = new BufferedReader(insrerr); 

      while (true) { 
       String line = stdoutReader.readLine();`enter code here` 
       if (line == null) { 
        break; 
       } 
       System.out.println(line); 
      } 

      while (true) { 
       String line = stderrReader.readLine(); 
       if (line == null) { 
        break; 
       } 
       System.out.println(line); 
      } 

      sess.close(); 

      conn.close(); 

     } catch (IOException e) { 
      e.printStackTrace(System.err); 
      System.exit(2); 
     } 
    } 
} 

    enter code here