in multithreaded web appliation if a session1 uploads a file with name test.txt this file will be saved in the above specified directory , at the same time another session2 uploads a file with the same name , so now the file which was already in temp folder because of session1 will be overridden with this new file , session1 when it tries to persists this file into database it is persisting the wrong file , Please advice me how to save files to temp folder in a multi-threaded web application, help is appreciated .
Paul Clapham wrote:The static method File.createTempFile appears to guarantee unique names, if I read the documentation correctly.
Once I move the file to database should I also delete the file from temp folder ? will this help ? what if user never saves and closes the browser , how will I be able to delete the file then ?
Rob Camick wrote:Crossposted: http://forums.oracle.com/forums/thread.jspa?messageID=9240911
Be Forthright When Cross Posting To Other Sites
sorry about that , I was looking for a n answer immediately , so I posted everywhere , I initially posted in wicket forums , where I did not receive any response , then I decided to try with java ranch and java forums. sorry , I did not mean to offend your effort , your help is very much appreciated.
Make sure that each thread creates a unique name. You can use the database to generate sequence numbers, or a GUID mechanism if you don't want to get the database involved.
personally , i think that this solution is more appropaite .
If there is a problem for you going this way then ,
For the Second Thread before saving the File in the Temp folder , get all the List of File names using in that directory using java.io.File.list , which returns you a String of Files and continue with your operation , whetehr to overirde it or appending some unique charter to it depending on Thread Id and Proceed .