播放框架 Assets.at 到 java.io.File
Play Framework Assets.at to java.io.File
在 Play Framework 中我们有一个辅助方法 controllers.Assets.at(String, String, boolean)
as described here.
此外,在 Play Framework 中,described here ok()
方法包含帮助程序,当在其内部提供 java.io.File
时自动计算必要的响应 headers这个:
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()
方法描述字符串中的 object,而不是 object 的内容。
Assets
与结果一起使用,但您可以查看 Application
class 而不是
// 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);
}
您不必将这两者结合起来。如果您正确配置了 Play framework
,您应该在 routes
文件中包含以下 route
:
GET /assets/*file controllers.Assets.at(path="/public", file)
您的客户可以通过以下请求使用它来获取 test.mp3
:
GET /assets/music/test.mp3
这适用于开发环境和生产环境:
Play.resourceAsStream("public/...", Play.current()).get()
在 Play Framework 中我们有一个辅助方法 controllers.Assets.at(String, String, boolean)
as described here.
此外,在 Play Framework 中,described here ok()
方法包含帮助程序,当在其内部提供 java.io.File
时自动计算必要的响应 headers这个:
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()
方法描述字符串中的 object,而不是 object 的内容。
Assets
与结果一起使用,但您可以查看 Application
class 而不是
// 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);
}
您不必将这两者结合起来。如果您正确配置了 Play framework
,您应该在 routes
文件中包含以下 route
:
GET /assets/*file controllers.Assets.at(path="/public", file)
您的客户可以通过以下请求使用它来获取 test.mp3
:
GET /assets/music/test.mp3
这适用于开发环境和生产环境:
Play.resourceAsStream("public/...", Play.current()).get()