我需要创建临时目录,但是当我尝试将文件创建到临时目录时,我总是访问被拒绝。在Tomcat中写入临时文件
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服务器内存上创建临时文件?我知道这是奇怪的,但我不知道......也许这是可能的。
2012-09-28
AmiraGL
+1
尝试使用ByteArrayOutputStream类而不是FileOutputStream,它应该允许您将数据写入内存中的缓冲区而不必使用文件。虽然我不了解Java,所以无法提供更多帮助。 –
+0
@Oscar在将数据写入ByteArrayOutputStream后可以将它转换为File? –
+0
'Tomcat服务器内存'对你的问题有什么用? –