57

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.

Jacob
  • 14,463
  • 65
  • 207
  • 320
  • see whether directory is present at "D:/Data" – code_fish Oct 24 '13 at 09:01
  • 2
    I would guess it is because you try to open the same file on each iteration of your `for` loop. – Francis Oct 24 '13 at 09:01
  • It's difficult to say without seeing the code which actually saves the file, or without code that will compile and reproduce the problem, but I'm guessing maybe the file already exists (or a directory exists with that name)? Also, note that on Windows files are not case sensitive. – Mikkel Løkke Oct 24 '13 at 09:03
  • you should use File.separatorChar to make your code as per standard instead os using '/' or '\'. – sudmong Oct 24 '13 at 09:08
  • No line of the above code can throw a `FilNotFoundException`. Where exactly does the exception being thrown and what is the code around it? – jboi Oct 24 '13 at 09:10

5 Answers5

105

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());
Neuron
  • 5,141
  • 5
  • 38
  • 59
Julien
  • 2,544
  • 1
  • 20
  • 25
  • Just a note, the opposite can also be true. I once wrote a directory as a file to a zip and when unzipping I got this error (because that directory already existed) – hayden.mumm Apr 06 '23 at 13:49
22

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();
} 
Neuron
  • 5,141
  • 5
  • 38
  • 59
Yuriy N.
  • 4,936
  • 2
  • 38
  • 31
8

I have search for this problem and i got the following answers:

  1. "C:\Program Files\Apache-tomcat-7.0.69\" remove the extra backslash (\)
  2. 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

Neuron
  • 5,141
  • 5
  • 38
  • 59
6

You need to set permission for the user controls .

  1. Goto C:\Program Files\
  2. Right click java folder, click properties. Select the security tab.
  3. There, click on "Edit" button, which will pop up PERMISSIONS FOR JAVA window.
  4. 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).
  5. 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.
  6. Finally, Hit apply and okay.
UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
Subrata
  • 93
  • 1
  • 1
4

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());
Neuron
  • 5,141
  • 5
  • 38
  • 59
Stepan
  • 1,391
  • 18
  • 40