gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:00:22 25 4
gpt4 key购买 nike

能否请您告诉我任何可用于将文件存储在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com