java - 将文件临时存储在tomcat的side temp文件夹中

标签 java tomcat temp

能否请您告诉我任何可用于将文件存储在 Tomcat 临时文件夹中的 Java 代码,以便一旦使用(下载)它就会自动删除?

最佳答案

有很多方法可以做到这一点,您应该探索 CATALINA_HOME 环境变量,因为它指向 Tomcat 安装目录。

更新 *试试这个:*

@SuppressWarnings("restriction")
 BASE64Decoder decoder = new BASE64Decoder(); // Decode encoded string into original byte array
System.out.println("in try "); 
System.out.println("imagestring "+ imageString);
byte[] decoded = decoder.decodeBuffer(imageString);
System.out.println("image"+ decoded); 
BufferedImage image = ImageIO.read(new ByteArrayInputStream(decoded));

File f = new File(System.getenv("CATALINA_HOME") + "/temp");//TomcatHome director/tempfolder
System.out.println(f.getAbsolutePath());//debug like this
if(!f.exists()){
f.mkdir();//make temp folder if it does not exist
}
ImageIO.write(image, "jpg",new File(f.getAbsolutePath() + "/yourimagename.jpg"));//write image to to the temp folder
}
//do some other Processing
File file = new File(f.getAbsolutePath() + "/yourimagename.jpg");
if(file.exists()){
file.delete();
 }

处理完成后,可以按常规方式删除file.delete();

您需要确保环境变量CATALINA_HOME 指向Tomcat 基目录。

关于java - 将文件临时存储在tomcat的side temp文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959142/

相关文章:

java - 使用 android TimerTask 和 Timer 防止最终用户操纵计时器

java - Apache Commons XMLConfiguration - 如何获取给定节点的对象列表?

apache - 使用apache代理tomcat,不能做sendRedirect

spring - @PreDestroy 未在 tomcat 关闭时调用 session 范围的 Spring bean

python - Python 2.7 中的 tempfile.TemporaryDirectory 上下文管理器

windows - Vista 环境变量中的 %TMP% 和 %TEMP% 有什么区别?

java - DateTime 不显示 UtcNow 选项

Java 平均水平及以上

tomcat - 为所有 webapps 定制 webapp 类加载器

Java临时文件的读写