Can you please tell me any Java code that I can use to store a file inside Tomcat's temp folder so that once it is used (downloaded) it will be deleted automatically?
Asked
Active
Viewed 3,978 times
-1
-
1One , your question is quite vague, and then we do not provide code, you write your own code, then ask where you got stuck. Welcome. – Stanley Mungai Apr 09 '14 at 10:18
-
You want to store this in the image in the temp folder?? – Stanley Mungai Apr 09 '14 at 10:42
-
@SuppressWarnings("restriction") BASE64Decoder decoder = new BASE64Decoder(); // Decode encoded string into original byte arraySystem.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)); this is the code which I used to convert a string to bufferImage I want to store that image to temp folder of tomcat ,once i used it it will automatically deleted – Ashish Bhasker Apr 09 '14 at 10:45
-
File file = new File("temp.png"); ImageIO.write(image, "png", file); String path = file.getCanonicalPath(); System.out.println("path:"+path); JSONObject obj=JSONFactoryUtil.createJSONObject(); obj.put("path",path); response.getWriter().write(obj.toString()); Above code I use to store that image in CanonicalPath From their I can use that image ,but once I use that image I have to delete that Image Manually ,so I want to store that image inside Temp folder of tomcat temporarily Plese help me – Ashish Bhasker Apr 09 '14 at 10:52
-
Yes I want to store that image in the temp folder of Tomcat – Ashish Bhasker Apr 09 '14 at 10:54
-
See My Updated Answer. it might give you an idea – Stanley Mungai Apr 09 '14 at 10:55
2 Answers
2
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.

Stanley Mungai
- 4,044
- 30
- 100
- 168
-
Hi I am Getting this Exception java.io.FileNotFoundException: D:\IWM\Softwares\eclipse\null\temp\yourimagename.jpg (The system cannot find the path specified) – Ashish Bhasker Apr 09 '14 at 11:28
-
What is your `CATALINA_HOME` value set to? Alternatively, you can define your own Environmental variable. It should Point to Tomcat Directory. Example `C:\apache-tomcat-7.0.39`. Debug your code as I have Indicated(see edit) `System.out.println(f.getAbsolutePath());` – Stanley Mungai Apr 09 '14 at 11:31
-
What Is that Line Printing Out? It was not meant to solve the Problem but to give you an idea where the Application is Trying to read. – Stanley Mungai Apr 09 '14 at 12:19
-
No I am telling that I define Environment variable ,but its not working That line print D:\IWM\Softwares\eclipse\null\temp instead of tomcat temp folder – Ashish Bhasker Apr 09 '14 at 12:58
-
Your environmental Variable is wrong. Set it Properly to point to Tomcat Folder like I have Shown Above. Restart your PC to pick the new env variable and it will work. I have the same sample code running in my machine. Working perfectly. – Stanley Mungai Apr 09 '14 at 13:05
-
-
`delete();` funrion will work anywhere. After you have done your processing just delete the image file. Please Accept the Answer if it worked for you. See my Updated Answer for the delete part. – Stanley Mungai Apr 10 '14 at 05:53
-
Hi Stanley Can you Please tell me Instead of File f = new File(System.getenv("CATALINA_HOME") + "/temp"); we can use other Thing so that It will support for all server not only for Tomcat server. Thanks In Advance – Ashish Bhasker Apr 11 '14 at 06:50
-
Yes!!!! Definitely. You can use Anything depending on what you have set to be your Environmental variable. This will work with Anything, but as you develop in Java web development, you may want to switch to Context Paths. – Stanley Mungai Apr 12 '14 at 07:13
-2
In jsp, this line gives temp folder path, You can use this path to store file <%=System.getProperty("java.io.tmpdir")%>

vishal
- 309
- 3
- 6
- 21
-
1The question was asked to store the file inside Tomcat folder. It is bad habit to write code snippets inside JSPs. – Stanley Mungai Apr 09 '14 at 10:21
-
Same property can be used in java file, it also gives path to tomcat temp folder – vishal Apr 09 '14 at 10:26