1

I need to create temporary directory but I'm always getting access denied when I try to create a file into the temporary directory.

java.io.FileNotFoundException: C:\tmpDir7504230706415790917 (Access Denied)

here's my code:

public static File createTempDir() throws IOException {
    File temp = File.createTempFile("tmpDir", "", new File("C:/"));

    temp.delete();
    temp.mkdir();
    return temp;
}

public File createFile(InputStream inputStream, File tmpDir ) {
    File file = null;
    if (tmpDir.isDirectory()) {
        try {
            file = new File(tmpDir.getAbsolutePath());
            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(file);

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            inputStream.close();
            out.flush();
            out.close();

            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } 
    }
    return file;
}

I'm working on a web application and I'm using tomcat. Is there a way to create temporary file on tomcat server memory? I know that's bizarre, but I don't know ... maybe it's possible.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Amira
  • 3,184
  • 13
  • 60
  • 95
  • 1
    Try the ByteArrayOutputStream class instead of FileOutputStream, that should let you write data into a buffer in memory without having to use a file. I don't know Java though so can't offer much more help. – 123 Sep 28 '12 at 16:24
  • @Oscar after writing data in a ByteArrayOutputStream can i convert it to File? – Amira Sep 28 '12 at 16:53
  • What does 'Tomcat server memory' have do do with your question? – user207421 Sep 29 '12 at 00:35
  • @EJP i meant is there a way to use tomcat to upload java.io.File and then use it , it's lik using tomcat to store files temporary and deleting them after use . Many thanks – Amira Sep 29 '12 at 07:26
  • In other words memory has nothing to do with it. You are looking for a way to store temporary files. I suggest you amend your title and text. – user207421 Sep 29 '12 at 08:08
  • Have you tried the solutions from this question? http://stackoverflow.com/questions/617414/create-a-temporary-directory-in-java – David Carboni May 09 '13 at 16:06

4 Answers4

13

You could use Tomcat's temp folder.
If you use

<%=System.getProperty("java.io.tmpdir")%>  

in a JSP you can get path to it.

rickz
  • 4,324
  • 2
  • 19
  • 30
4

This line in your code says create a file whose name starts with text "tmpDir" in the directory "C:\". That is not what you want.

File temp = File.createTempFile("tmpDir","",new File("C:/"));

The operating system is properly disallowing that because C:\ is a protected directory. Use the following instead:

File temp = File.createTempFile("tmp",null);

This will let Java determine the appropriate temporary directory. Your file will have the simple prefix "tmp" followed by some random text. You can change "tmp" to anything meaningful for your app, in case you need to manually clean out these temp files and you want to be able to quickly identify them.

Guido Simone
  • 7,912
  • 2
  • 19
  • 21
  • i tried it but i got this java.io.FileNotFoundException: C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\tmpDir8076115893277193438.tmp (Access Denied) – Amira Sep 28 '12 at 16:37
  • I think I see the problem. Which line of your code is causing this error? – Guido Simone Sep 28 '12 at 16:43
  • Looking at the bigger picture, I think rickz is onto something. You don't need any of this code to create a temp directory. Just use the system defined temp directory. However if you still want to use the code, you need to change "return temp" to "return temp.getParentFile()". Your method is returning a file (which you just deleted) not a directory. – Guido Simone Sep 28 '12 at 16:49
  • i change it like this: File temp = File.createTempFile("tmpDir","",new File("C://Documents and Settings//Administrateur//") ); return temp; the access denied error disappears but the file was not created – Amira Sep 28 '12 at 17:04
  • If you use user1657364 second line of code, then Tomcat's temp folder will be used. – rickz Sep 29 '12 at 02:10
  • @user1657364 it's me again this line `File temp = File.createTempFile("tmpDir",null,new File(System.getProperty("user.home")) ); ` creates a file successfully but is there a way to create a temporary folder ? Many thanks :) – Amira Sep 29 '12 at 07:34
1

You usually cannot write onto C:\ directly due to the default permission setting. I sometime have permission issue for doing so. However, you can write your temporary file in your user folder. Usually, this is C:\Documents and Settings\UserName\ on XP or C:\Users\UserName\ on vista and Windows 7. A tool called SystemUtils from Apache Lang can be very useful if you want to get the home directory depending on OS platform.

For example:

SystemUtils.getUserDir();
SystemUtils.getUserHome();

Update

Also, you create a temp file object but you call mkdir to make it into a directory and try to write your file to that directory object. You can only write a file into a directory but not on the directory itself. To solve this problem, either don't call temp.mkdir(); or change this file=new File(tmpDir.getAbsolutePath()); to file=new File(tmpDir, "sometempname");

gigadot
  • 8,879
  • 7
  • 35
  • 51
0

On Linux with tomcat7 installation:

So if you are running web application this is the temp directory Tomcat uses for the creation of temporary files.

TOMCAT_HOME/temp

In my case TOMCAT_HOME => /usr/share/tomcat7

If you are running Java program without tomcat, by default it uses /tmp directory.

Not sure if it affects but i ran this command too.

chmod 777 on TOMCAT_HOME/temp
mdev
  • 1,366
  • 17
  • 23