Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any good strategy of storing randomly generated file?

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My servlet will generate a different file upon each request, and I want the user to be able to download this file by clicking on a link.
What I am doing right now is to generate a random file name and save this newly generated file with that random file name under a particular directory.
But then, the problem is obvious: With more and more requests coming in, I will have a huge number of files under that directory.
So, do you have a better idea of handling this situation?
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gene,
one thing u can try is create a sub-directory for each user in current directory (which ur using)
and store the randomly generated file in his own directory!!!
regards
Praful
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just for common knoweledge, what do you need this files-creating strategy for?
 
Chris Smith
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this information really need to live very long, or is it just to make the download link work? If it only needs to live long enough for this download link, and the data is not very large, then I'd just store the data directly in the session (as, eg, a byte[]).
If you're using a file because the size of the data is too large to keep it in memory, then you could use the following scheme to make sure you've got the minimum possible number of files:
1. When you create a file, immediately set it to delete on exit, so that unexpected shutdowns of the server will clean up after themselves.
2. Add the file to the session, but wrap it in an object that implements HttpSessionBindingListener. Have it delete the file when it is unbound from the session.
That should go as far as you can toward ensuring that these files are cleaned up on a timely basis.
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
Thanks. You've got exactly what I want.
Yes, the information does not need to live very long, just good enough for the link to work, because on the server side, I am gonna send the data to a database (or probably a LDAP).
Yes, the data is not very large, probably just about 1K bytes.
So just store it in the session as byte[]? How? Would you give me a hint?
Thanks.
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Praful,
Thanks, but I guess your strategy does not solve my problem. Because if you don't efficiently remove those randomly-generated files, they are still taking a lot of disk space no matter where you save them, right?
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asher,
Just for your information, I am working on a certification authority (CA) project, in which the CA will have to generate a certificate for each client who so requests. And after the certificate is generated, the CA somehow will have to let the client get the certificate and install it into its browser.
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gene-
ok, i c now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic