简体   繁体   中英

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.

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();

You need to make sure that the environmental variable CATALINA_HOME Points to Tomcat base directory.

在jsp中,这一行给出了临时文件夹路径,您可以使用此路径来存储文件<%=System.getProperty("java.io.tmpdir")%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM