我需要创建临时目录,但是当我尝试在临时目录中创建文件时总是被拒绝访问。
java.io.FileNotFoundException: C:\tmpDir7504230706415790917 (Access Denied)
这是我的代码:
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;
}
我正在开发一个 Web 应用程序,并且正在使用 tomcat。有没有办法在tomcat服务器内存上创建临时文件?我知道这很奇怪,但我不知道……也许有可能。