我如何获得和创建访问权限,群组和用户在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. SQL为PC上的所有用户分配读/写访问权限
- 2. PC上的JarBundler
- 3. 如何使用pc的pc访问localhost?
- 4. 保护用户PC上的应用程序数据库访问
- 5. Git设置 - 旧PC上的新用户
- 6. 如何让sqlcmd在linux上拥有文件访问权限?
- 7. 如何从浏览器访问客户端PC上的设备
- 8. 如何获得在PC上的所有用户在Java
- 9. 如何从另一台PC上将Linux目录挂载到本地Linux PC上?
- 10. 在使用wifi连接的不同PC上访问localhost xampp
- 11. 使用Java的PC到PC通信
- 12. HIBERNATE和HSQLDB - 在PC上运行 - 在Linux上出错
- 13. 在用户PC上存储设置
- 14. 在MAC和PC上的语音识别
- 15. 在PC和IOS上工作的iframe
- 16. Linux/RHEL5:IPv6上的UDP不能在同一台PC上工作
- 17. 根据登录用户的访问权限限制PHP的文件访问
- 18. 访问远程PC
- 19. 在Linux Centos上创建第二个用户权限的文件夹/文件
- 20. 如何从外部访问在我的PC上运行的Java servlet?
- 21. 在Android中写入文件可通过局域网上的PC访问
- 22. 如何使用客户端PC上的Web应用程序访问UDP端口?
- 23. Android将桌面PC上的文件上传到Android设备
- 24. 将报告保存到客户端PC上的文件?
- 25. @ PC上的font-face别名问题
- 26. 存储在Linux的PC的任何手机上查看网站
- 27. 在VSS中的文件上分配访问权限
- 28. XP上的虚拟PC
- 29. 锁定PC上的Findwindow,FindWindowEx和SendMessage
- 30. 在新PC上的SQL应用程序
你试过了什么? – m0skit0 2013-02-20 16:12:51