java 如何使用Java代码将图像权限模式更改为777?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/986830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:42:03  来源:igfitidea点击:

How to change image permission mode to 777 using Java code?

javanio

提问by Bill the Lizard

I want to give permissions mode value "777" to image file using Java code. How can I give that using Java? Because I can't delete the image with default permission mode "664".

我想使用 Java 代码为图像文件提供权限模式值“777”。我怎样才能使用 Java 提供它?因为我无法删除默认权限模式“664”的图像。

回答by Kieveli

You can use the 'exec' method to run an external command to do the chmod.

您可以使用 'exec' 方法运行外部命令来执行 chmod。

Runtime.getRuntime().exec( "chmod 777 myfile" );

回答by Bill the Lizard

You can create a Fileobject associated with the file then change permissions using setExecutable, setReadable, and setWritable. Naturally, these operations will fail if your program doesn't have permission to change access permissions for the file.

您可以创建与文件关联的File对象,然后使用setExecutablesetReadablesetWritable更改权限。当然,如果您的程序无权更改文件的访问权限,这些操作将失败。

回答by z -

Its a part of the NIO package. You can do it via java.nio.file.attribute.PosixFilePermission.

它是 NIO 包的一部分。您可以通过java.nio.file.attribute.PosixFilePermission 来完成

This is only in Java 1.7though, it has been a known issuefor a while now.

这只是在Java 1.7 中,它已经是一个已知问题一段时间了。

回答by Michael Zilbermann

Unfortunately Java gives few grab to the system file properties, because it would break the gold rule of java "write once, run everywhere".

不幸的是,Java 几乎没有抓住系统文件的属性,因为它会打破 Java“一次编写,到处运行”的黄金法则。

Indeed when you write that you want to put a 777 access to the file, it obviously means that you're under unix. But what such a request would have meant under Windows for example ?

实际上,当您写下要对文件进行 777 访问时,这显然意味着您在 unix 下。但是这样的请求在 Windows 下意味着什么?

So it shouldn't be a surprise that to address specific OS topics, you would have to use specific functions. That's the price to pay when using a language that claims (with strong reasons) being "platform independent"

因此,为了解决特定的操作系统主题,您必须使用特定的函数,这不足为奇。这是使用声称(有充分理由)“独立于平台”的语言时要付出的代价

The safer way would be to use a JNI library that implements exactly what you want. A simple but dirty and hazardous way would be to using a Runtime.getRuntime().exec("chmod 777 myFile").

更安全的方法是使用完全实现您想要的 JNI 库。一种简单但肮脏且危险的方法是使用 Runtime.getRuntime().exec("chmod 777 myFile")。

Why hazardous ? In fact, what happens when one call such exec("blahblah") ?

为什么危险?事实上,当一个人调用这样的 exec("blahblah") 时会发生什么?

The unix process running your java program forks; the "duplicated" process then execs a shell (ksh ? csh ? bash ?? it depends on your unix configuration), that tries to understand and run your "blahblah" sentence.

运行你的 java 程序的 unix 进程分叉;“重复”进程然后执行一个 shell(ksh?csh?bash ??这取决于你的 unix 配置),它试图理解并运行你的“blahblah”句子。

If your jvm is actually using 500 MB, the forked process would be the same size when it starts...

如果您的 jvm 实际使用了 500 MB,那么分叉进程在启动时的大小将相同...

Then the shell will start with the same "shell context" than the java process, that is to say the same PATH, aliases and so on. Even "chmod" could have been aliased, so it would be better to call the full path for chmod (/bin/chmod, /usr/bin/chmod... it depends on the unix flavour...).

然后shell 会以与java 进程相同的“shell 上下文”开始,也就是说相同的PATH、别名等。甚至“chmod”也可能是别名,因此最好调用 chmod 的完整路径(/bin/chmod、/usr/bin/chmod...这取决于 unix 风格...)。

During the shell execution, your java program is freezed, and you have no way to unfreeze it if for any reason the shell hangs.

在 shell 执行期间,您的 java 程序被冻结,如果由于任何原因 shell 挂起,您将无法解冻它。

And finally, it's not even sure the unix owner of the java program has sufficient rights to change such informations on the file (he could be allowed to create the file, but not to change user level rights, for example).

最后,甚至不确定 java 程序的 unix 所有者是否有足够的权限来更改文件上的此类信息(例如,他可以被允许创建文件,但不能更改用户级别权限)。

Maybe you try to handle at the java level a problem that should be addressed at the OS level ?

也许您尝试在 Java 级别处理应该在操作系统级别解决的问题?

回答by A_Var

Here goes the code:-

这是代码:-

List<String> chPermList = new LinkedList<String>();
chPermList.add("/bin/chmod");
chPermList.add("777");
chPermList.add(Path_to_image_file);


    public void changePermission(List chPermList)
    {
        ProcessBuilder pb = null;
        pb = new ProcessBuilder(chPermList);
                pb.redirectErrorStream(true);

                Process p = pb.start();

                BufferedReader br = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));

                p.waitFor();

                if(p.exitValue()!=0)
                {
                    strExpMsg = "The permission change failed with exit value" + p.exitValue();

                }
    }