Handling dynamic created files in play 2

2019-02-15 08:48发布

I wrote a small app that creates downloadable pdf files with play 2.0

I want to serve them to the public. On my development environment I created a folder in the /assets/ folder and everything worked nice.

Now, when switching to production, I figured out that play always deployed those files behind my back.

Do I really have to write a own controller to serve those files or what is the way here ?

1条回答
手持菜刀,她持情操
2楼-- · 2019-02-15 09:38

I've also had problems trying to serve files created dynamically with the assets controller. I don't know if it's for some kind of caching but I ended up writing my own controller, and now it woks perfectly.

I mean, I use the Assets controller for regular public files and for my dynamic generated files I use this one:

public class FileService extends Controller {
       static String path = "/public/dynamicfiles/";
       public static Result getFile(String file){
              File myfile = new File (System.getenv("PWD")+path+file);
              return ok(myfile);
       }
}

And the routes would be like this:

GET     /files/:file           controllers.FileService.getFile(file: String)
GET     /assets/*file               controllers.Assets.at(path="/public", file)

It works great for me

查看更多
登录 后发表回答