2011-03-08 99 views
2

我有一個文件上傳Servlet和嘗試一些文件存儲爲:GWT文件上傳java.security.AccessControlException:訪問被拒絕(java.io.FilePermission中

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) 

我該如何解決呢?

回答

2

你既可以使/可寫你的應用服務器(這似乎並不像一個偉大的想法對我來說)或存儲上傳編輯文件在另一個可寫的位置。

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

您似乎也試圖聲明uploadedFile兩次。從錯誤,它看起來像UPLOAD_DIRECTORY/或您實際上使用第二行。也許你只需要在UPLOAD_DIRECTORY提供正確的位置?

要回答關於Java策略文件的註釋的問題:

Security and Permissions 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.

見在政策文件中設置文件系統權限的File Permission部分。

爲了重申我自己的意見,您還需要注意操作系統中的文件系統權限,其中的控制取決於您使用的操作系統。

+0

我使用eclipse與Jetty服務器,所以如何設置寫權限? – 2011-03-09 08:20:14

+0

@gwt_user我正在考慮文件系統權限,實際上。但是如果你想改變Java策略文件,你可以看看這裏:[Security and Permissions](http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html#FilePermission) – 2011-03-09 15:00:11

+0

@IsaacTruett where我必須把文件放在我的eclipse項目中,我需要填寫什麼? – 2011-03-09 16:01:41

相關問題