问题
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.)
What could be the reason and how can I resolve this? I do have read and write permission on this folder.
回答1:
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.
Try with something like
File file = new File("D:/Data/" + item.getFileName());
回答2:
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
File file = new java.io.File(path);
//file.mkdirs(); // wrong!
file.getParentFile().mkdirs(); // correct!
if (!file.exists()) {
file.createNewFile();
}
回答3:
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.
Your problem will be solved
回答4:
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.
回答5:
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());
来源:https://stackoverflow.com/questions/19561386/java-access-is-denied-java-io-filenotfoundexception