[英]Store a file temporarily in side temp folder of tomcat
你能告訴我任何可以用來在 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 基目錄。
在jsp中,這一行給出了臨時文件夾路徑,您可以使用此路徑來存儲文件<%=System.getProperty("java.io.tmpdir")%>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.