0
java.io.FileNotFoundException: C:\Emails\ToSend\. (Access is denied)

I'm trying to download a file from FTP and save it to a folder to be processed, But when I setup the OutputStream it throws this error. Here is the code:

File downloadFile1 = new File("C:" + File.separator + "Emails"
                    + File.separator + "ToSend" + File.separator
                    + f.getName());
            OutputStream outputStream1 = new BufferedOutputStream(
                    new FileOutputStream(downloadFile1));

The FTPFile f is fetched by the FTPClient from the FTP server. I've got full control of the folder Emails and all of its sub folders and I have given these same permissions to all application packages.

I'm sure its just because I'm abit out of my depth when it comes to file permissions.

Any and all help appreciated

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Dart2112
  • 19
  • 6
  • The whole point of Java is to be platform independent. so you shouldn't hard-code nasty Windowsisms such as C:. In Linux and Mac there's no such thing as the C: drive. – NickJ Aug 27 '15 at 09:16
  • As you noted, this seems like the problem is with permissions and I would guess it is totally unrelated to FTP (but just to be sure, if I was in your place I would try to write a mini-application just to try to create a file in that location without any ftp connection. I think you should maybe add a little bit more info about OS on which you run the application. E.g., when you run under Windows, maybe you need to run the application as administrator. – Milan Nosáľ Aug 27 '15 at 09:19
  • 1
    Beside all other comments about platform independency. Have a look in the exception message `C:\Emails\ToSend\.` (seems f.getName() returns `.`). This is a directory not a file. You can't create an outputstream for a directory entry. – SubOptimal Aug 27 '15 at 09:22
  • The program is only going to be run on my windows PC so I'm not worried about it being cross-platform capable, I'll test creating a file there and report back – Dart2112 Aug 27 '15 at 09:24
  • You *don't* have create permissions in the directory named in the exception. Whatever you may think. – user207421 Aug 27 '15 at 09:27
  • and how would I go about granting these permissions? – Dart2112 Aug 27 '15 at 09:34
  • In windows you can go to the directory, right click it, properties, security and from there you can assign permissions – baarkerlounger Aug 27 '15 at 09:41
  • But who do I assign them to? I've given "All Application Packages" full control as well as all the other users listed. – Dart2112 Aug 27 '15 at 09:43
  • 1
    You do not understand what "Access denied" means? – Raedwald Aug 27 '15 at 10:36
  • Printout `f.getName()` and check out what you are getting. I suspect you are not getting the file name instead you are getting a '.' – Dakshinamurthy Karra Aug 27 '15 at 10:40
  • I understand what it means, I just didn't understand exactly what was causing it – Dart2112 Aug 27 '15 at 10:51

1 Answers1

0

Instead of hard-coding the file location, how about using a temporary directory? This is assuming the file will be processed immediately by your application and does not need to be kept.

For how to do this, see the accepted answer to this question: How to create a temporary directory/folder in Java?

Community
  • 1
  • 1
NickJ
  • 9,380
  • 9
  • 51
  • 74