簡體   English   中英

Java中Linux PC上文件的訪問權限,組和用戶

[英]Access permission ,group and user of a file on linux pc in java

我如何在Java中的Linux PC上獲取和創建文件的訪問權限,組和用戶。

如果您使用的是Java 7,則應該能夠使用POSIX功能來執行此操作。 具體來說, getPosixFilePermissions方法應該是您要尋找的方法。 有關更多信息,請參見此問題

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", "ace@dir", linuxCommands[0] + fileName);
        excuteLinuxCommand("192.168.0.26", "root", "ace@dir", linuxCommands[1] + fileName);
        excuteLinuxCommand("192.168.0.26", "root", "ace@dir", linuxCommands[2] + fileName);
        excuteLinuxCommand("192.168.0.26", "root", "ace@dir", 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM