在Tomcat中编写临时文件

Ami*_*ira 1 java file-io tomcat temporary-files

我需要创建临时目录,但是当我尝试在临时目录中创建文件时,我总是被拒绝访问.

java.io.FileNotFoundException: C:\tmpDir7504230706415790917 (Access Denied)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我正在开发一个Web应用程序,我正在使用tomcat.有没有办法在tomcat服务器内存上创建临时文件?我知道这很奇怪,但我不知道......也许这是可能的.

ric*_*ckz 13

您可以使用Tomcat的临时文件夹.
如果你使用

<%=System.getProperty("java.io.tmpdir")%>  
Run Code Online (Sandbox Code Playgroud)

在JSP中,您可以获得它的路径.