[英]Store a file temporarily in side temp folder of tomcat
你能告诉我任何可以用来在 Tomcat 的临时文件夹中存储文件的 Java 代码,以便一旦使用(下载)它会被自动删除?
Well there are Many ways to do this, you should explore the CATALINA_HOME
Environmental Variable as this Points to the Tomcat Installation Directory.那么有很多方法可以做到这一点,您应该探索
CATALINA_HOME
环境变量,因为它指向 Tomcat 安装目录。
Update * Try this: *更新*试试这个: *
@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();
}
After processing , you can delete it in the usual way file.delete();
处理完后,可以用通常的方式删除它
file.delete();
You need to make sure that the environmental variable CATALINA_HOME
Points to Tomcat base directory.您需要确保环境变量
CATALINA_HOME
指向 Tomcat 基目录。
在jsp中,这一行给出了临时文件夹路径,您可以使用此路径来存储文件<%=System.getProperty("java.io.tmpdir")%>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.