Question

I'm trying to create a feature in my program that allows users to download their data into a file.

Right now, I have a method in one of my controllers that creates a File object with a certain name and uses a FileWriter to load the content into the file. After it is finished, the File object is passed into renderBinary(). This sends the file to the user as expected.

However, this saves the file inside my Play! project directory before it sends. This leads me to wonder what will happen when there are many users trying to generate and download files. Will they overwrite each other?

So in short, what's the best way to generate files and send them via renderBinary?

Était-ce utile?

La solution

In a multi-user environment, it is bad practice to save file data to a constant filename. You do run the risk that in a separate running thread, your file may get overwritten, or get an IO exception due to another file handle being open.

A better approach would be to generate a random hash for the filename on your server side, and use that as your filename.

Autres conseils

File f = File.createTempFile("prefix", "suffix");       
f.deleteOnExit();
// put contents into the file
renderBinary(f);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top