在 Play 框架中,我们有一个辅助方法 controllers.Assets.at(String, String, boolean)
as described here .
同样在 Play 框架中它是 described here ok()
方法包含帮助程序,当在其中提供 java.io.File
时,这些帮助程序会自动计算必要的响应 header ,如下所示:
public Result index() {
return ok(new java.io.File("/tmp/fileToServe.pdf"));
}
我的问题是如何将两者结合起来。目前我有这样的东西:
public static Result index() {
return ok(new File(controllers.Assets.at("public/", "music/test.mp3", false).toString()));
}
这显然不起作用,因为 controllers.Assets.at()
上的 toString()
方法描述的是字符串中的对象,而不是对象的内容。
最佳答案
Assets
可与 Result 配合使用,但您可以查看 Application
类
// Get the current app
final Application app = Play.application();
final File file = app.getFile("public/music/test.mp3");
更多信息here ,但实现是
/**
* Get a file relative to the application root path.
*
* @param relativePath relative path of the file to fetch
* @return a file instance - it is not guaranteed that the file exists
*/
default File getFile(String relativePath) {
return getWrappedApplication().getFile(relativePath);
}
关于java - 将 Framework Assets.at Play 到 java.io.File,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164736/