java 写成临时文件_在Tomcat中写入临时文件

我需要创建临时目录,但是当我尝试将文件创建到临时目录时,我总是访问被拒绝。在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服务器内存'对你的问题有什么用? –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值