java - 将 Framework Assets.at Play 到 java.io.File

标签 java playframework

在 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/

相关文章:

java - 应该在 equals/hashCode/toString 中使用 @Transient 属性吗?

java - 删除两个重复行

java - 使用 Play 框架的移动应用程序的不同 View

java - Play Framework 入口点

java - Play Framework 2.0.X 中的邮件表单

ssl - 负载平衡 IP OVH 和 Play 静态文件

java - Java 桌面应用程序中日志文件位置的最佳做法是什么?

java - BlueJ 库有错误

java - 如何单击网络元素中的随机链接

javascript - 将 fluentlenium 与动态生成的内容一起使用时出现问题