AccessControlException

AccessControlException

本文介绍了java.security.AccessControlException:访问被拒绝(java.io.FilePermission的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 最终文件parentDir = new File(S:\\PDSPopulatingProgram); 
parentDir.mkdir();
final String hash =popupateData;
final String fileName = hash +.txt;
final File file = new File(parentDir,fileName);
file.createNewFile(); //创建文件PDSPopulatingProgram / popupateData.txt

我试图在一个文件夹中创建一个文件,但是我我得到了异常:

$ p $ java.security.AccessControlException:访问被拒绝

我在Windows环境下工作。我可以从Windows资源管理器创建一个文件夹,但不能从Java代码创建。

我该如何解决这个问题?虽然不推荐这样做,但是如果你真的想让你的web应用程序访问部署目录之外的文件夹的话。您需要在 java.policy 文件中添加以下权限(路径与Petey B的回复一样)

<$ p $ b $ 权限java.io.FilePermission您的文件夹路径,写

在你的情况下,这将是

 权限java.io.FilePermissionS:/ PDSPopulatingProgram /  - ,write 

这里 / - 表示任何文件或在这个文件夹中的子文件夹。

警告:但是这样做会引发一些安全风险。

final File parentDir = new File("S:\\PDSPopulatingProgram");
parentDir.mkdir();
final String hash = "popupateData";
final String fileName = hash + ".txt";
final File file = new File(parentDir, fileName);
file.createNewFile(); // Creates file PDSPopulatingProgram/popupateData.txt

I am trying to create a file in a folder but I am getting exception as

java.security.AccessControlException: Access denied

I am working in windows environment. I can create a folder from the Windows Explorer, but not from the Java Code.

How can I resolve this issue?

解决方案

Although it is not recommended, but if you really want to let your web application access a folder outside its deployment directory. You need to add following permission in java.policy file (path is as in the reply of Petey B)

permission java.io.FilePermission "your folder path", "write"

In your case it would be

permission java.io.FilePermission "S:/PDSPopulatingProgram/-", "write"

Here /- means any files or sub-folders inside this folder.

Warning: But by doing this, you are inviting some security risk.

这篇关于java.security.AccessControlException:访问被拒绝(java.io.FilePermission的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:38