GWT 文件上传 java.security.AccessControlException: 访问被拒绝 (java.io.FilePermission

发布于 2024-10-20 14:40:41 字数 662 浏览 5 评论 0原文

我有一个文件上传 Servlet 并尝试将一些文件存储为:

try{
    File uploadedFile = new File(UPLOAD_DIRECTORY + fileName);
    File uploadedFile = new File("/"+fileName);
    item.write(uploadedFile);
}
catch (Exception e) {
    e.printStackTrace();
}

之后我收到以下错误:

java.security.AccessControlException: access denied (java.io.FilePermission /untitled.html write)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)

如何修复它?

I have a file upload Servlet and try to store some files as:

try{
    File uploadedFile = new File(UPLOAD_DIRECTORY + fileName);
    File uploadedFile = new File("/"+fileName);
    item.write(uploadedFile);
}
catch (Exception e) {
    e.printStackTrace();
}

After that I get the following error:

java.security.AccessControlException: access denied (java.io.FilePermission /untitled.html write)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)

How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森末i 2024-10-27 14:40:41

您可以使应用服务器可写 / (这对我来说似乎不是一个好主意)或将上传的文件存储在另一个可写位置。

File uploadedFile = new File(UPLOAD_DIRECTORY + fileName);
File uploadedFile = new File("/"+fileName);

您似乎还尝试声明 uploadedFile 两次。从错误来看,UPLOAD_DIRECTORY/ 或者您实际上正在使用第二行。也许您只需要在 UPLOAD_DIRECTORY 中提供正确的位置?

要回答有关 Java 策略文件的评论问题:

来自 安全性和权限java.sun.com 上的

有一个 Java 策略文件
平台安装(系统)和
每个用户的可选策略文件。
系统策略文件位于
{java.home}/lib/security/java.policy,
用户策略文件位于每个
用户的主目录。该系统和
用户策略文件被合并。所以对于
例如,可能有一个系统策略
授予很少权限的文件
系统上的所有用户,以及
个人政策文件授予
某些特定的附加权限
用户。

有关设置文件系统权限的信息,请参阅文件权限部分策略文件。

重申我自己的评论,您还需要注意操作系统中的文件系统权限,其控制取决于您所使用的操作系统。

You could either make / writable by your app server (which doesn't seem like a great idea to me) or store your uploaded files in another writable location.

File uploadedFile = new File(UPLOAD_DIRECTORY + fileName);
File uploadedFile = new File("/"+fileName);

You also appear to be trying to declare uploadedFile twice. From the error, it looks like either UPLOAD_DIRECTORY is / or you're actually using the second line. Perhaps you just need to supply the right location in UPLOAD_DIRECTORY?

To answer your comment-question about Java policy files:

From Security and Permissions on java.sun.com:

There is one policy file for Java
platform installation (system) and an
optional policy file for each user.
The system policy file is in
{java.home}/lib/security/java.policy,
and the user policy file is in each
user's home directory. The system and
user policy files are combined. So for
example,there could be a system policy
file with very few permissions granted
to all users on the system, and
individual policy files granting
additional permissions to certain
users.

See the File Permission section for setting file system permissions in the policy file.

And to reiterate my own comment, you'll also want to pay attention to file system permissions in the OS, control of which will depend on the OS you're using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文