我如何獲得和創建訪問權限,羣組和用戶在java的linux pc上的文件。訪問權限,在java pc上的linux pc上的文件的組和用戶
回答
如果您使用的是Java 7,則應該可以使用POSIX functionality來執行此操作。具體來說,getPosixFilePermissions
方法應該是你要找的。有關更多信息,請參閱this question。
謝謝你的回答。但我想在java中使用這個getPosixFilePermissions方法6 – 2013-02-20 16:35:22
它在Java 6中不可用。你可以升級到Java 7嗎? – 808sound 2013-02-20 16:36:09
因爲我們的開發框架是基於Java 6的,所以在我們的項目中不能使用Java 7。 – 2013-02-20 16:40:18
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
- 1. Linux上Java的PC硬件端口訪問
- 2. SQL爲PC上的所有用戶分配讀/寫訪問權限
- 3. PC上的JarBundler
- 4. 如何使用pc的pc訪問localhost?
- 5. 保護用戶PC上的應用程序數據庫訪問
- 6. PC和瘦客戶機上的Excel
- 7. Android和PC上的OpenGL ES
- 8. Git設置 - 舊PC上的新用戶
- 9. System.BadImageFormatException在PC上運行PC Sim軟件
- 10. 從Android設備,藍牙訪問Windows PC上的文件?
- 11. 以編程方式訪問Android設備上的文件從PC
- 12. 從PC通過USB訪問Android設備上的文件
- 13. Linux/CentOS PC上的php.ini文件在哪裏?
- 14. PC上的PlayStation 4軟件
- 15. 從用戶的js/html文件訪問sqlite數據庫PC
- 16. 文件訪問權限,即使在Linux
- 17. 如何讓sqlcmd在linux上擁有文件訪問權限?
- 18. 如何從瀏覽器訪問客戶端PC上的設備
- 19. 如何獲得在PC上的所有用戶在Java
- 20. PC上的Phonegap cordova.js
- 21. 如何從另一臺PC上將Linux目錄掛載到本地Linux PC上?
- 22. 的Git - GitHub的帳戶和Atlassian的藏匿帳戶在PC上
- 23. 從桌面PC上訪問SSRS 2008
- 24. 「會員」組用戶的訪問權限?
- 25. 限制Windows 7 PC上的UL流量
- 26. Linux文件權限和Java問題(權限保留)
- 27. 在使用wifi連接的不同PC上訪問localhost xampp
- 28. 使用Java的PC到PC通信
- 29. HIBERNATE和HSQLDB - 在PC上運行 - 在Linux上出錯
- 30. 寫入應用程序遠程訪問PC上的文本文件可能嗎?
你試過了什麼? – m0skit0 2013-02-20 16:12:51