• Post Reply Bookmark Topic Watch Topic
  • New Topic

saving uploaded file to temp folder in a multi-threaded web application

 
srini carry
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to save the uploaded file in temp folder till the its persisted in database, so when ever a file is uploaded my application writes it to temp folder ,the temp folder is



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 .
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static method File.createTempFile appears to guarantee unique names, if I read the documentation correctly.
 
srini carry
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 2700
10
 
srini carry
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just let us know when you do so, so that no one wastes time answering a question that may already be answered.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also let us (and them) know when you get answers on those other forums, again for the same reason.
 
srini carry
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And also let us (and them) know when you get answers on those other forums, again for the same reason.


thank you all for your help. I appreciate it. sure I will not post in multiple forums again.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's ok to post in multiple forums -- as long as you let everyone know when you do, and be sure to let everyone know when you get an answer. In fact, posting the link to the answer would be a very good thing to do.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!