Java - 访问被拒绝 java.io.FileNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19561386/
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
Java - Access is denied java.io.FileNotFoundException
提问by J?cob
I have the following code:
我有以下代码:
List<FileItem> items = uploadHandler.parseRequest(request);
for (FileItem item : items) {
if (!item.isFormField()) {
File file = new File("D:/Data");
}
}
When I am trying to save a file, I am getting the following error
当我尝试保存文件时,出现以下错误
java.io.FileNotFoundException: D:\Data (Access is denied.)
java.io.FileNotFoundException: D:\Data (Access is denied.)
What could be the reason and how can I resolve this? I do have read and write permission on this folder.
可能是什么原因,我该如何解决?我对这个文件夹有读写权限。
采纳答案by Julien
When you create a new File
, you are supposed to provide the file name, not only the directory you want to put your file in.
创建 new 时File
,您应该提供文件名,而不仅仅是要放置文件的目录。
Try with something like
尝试类似的东西
File file = new File("D:/Data/" + item.getFileName());
回答by yurin
Not exactly the case of this question but can be helpful. I got this exception when i call mkdirs() on new file instead of its parent
不完全是这个问题的情况,但可能会有所帮助。当我在新文件而不是其父文件上调用 mkdirs() 时出现此异常
File file = new java.io.File(path);
//file.mkdirs(); // wrong!
file.getParentFile().mkdirs(); // correct!
if (!file.exists()) {
file.createNewFile();
}
回答by Subrata
You need to set permission for the user controls .
您需要为用户控件设置权限。
- Goto C:\Program Files\
- Right click java folder, click properties. Select the security tab.
- There, click on "Edit" button, which will pop up PERMISSIONS FOR JAVA window.
- Click on Add, which will pop up a new window. In that, in the "Enter object name" box, Enter your user account name, and click okay(if already exist, skip this step).
- Now in "PERMISSIONS OF JAVA" window, you will see several clickable options like CREATOR OWNER, SYSTEM, among them is your username. Click on it, and check mark the FULL CONTROL option in Permissions for sub window.
- Finally, Hit apply and okay.
- 转到 C:\Program Files\
- 右键单击java文件夹,单击属性。选择安全选项卡。
- 在那里,单击“编辑”按钮,这将弹出 JAVA 权限窗口。
- 单击添加,这将弹出一个新窗口。在那,在“输入对象名称”框中,输入您的用户帐户名称,然后单击确定(如果已经存在,请跳过此步骤)。
- 现在在“JAVA 权限”窗口中,您将看到几个可点击的选项,如 CREATOR OWNER、SYSTEM,其中包括您的用户名。单击它,然后在子窗口的权限中选中完全控制选项。
- 最后,点击申请就可以了。
回答by Subrata
I have search for this problem and i got the following answers:
我搜索了这个问题,得到了以下答案:
"C:\Program Files\Apache-tomcat-7.0.69\"
remove the extra backslash (\
)- Right click the log folder in tomcat folder and in security tab give this folder as a write-permission and then restart the net-beans as an run as administrator.
"C:\Program Files\Apache-tomcat-7.0.69\"
删除多余的反斜杠 (\
)- 右键单击 tomcat 文件夹中的日志文件夹,并在安全选项卡中将此文件夹作为写权限,然后以管理员身份重新启动 net-beans。
Your problem will be solved
你的问题会得到解决
回答by Stepan
Make sure that the directory exists, you have permission to access it and add the file to the path to write the log:
确保该目录存在,您有权访问它并将文件添加到写入日志的路径:
File file = new File("D:/Data/" + item.getFileName());