我们有大量的图片需要存储在play应用程序文件夹之外的外部路径中。如何将其作为资产使其能够在web服务器上流式传输?
conf/routes
中,你需要添加一行代码来引入你的外部资产:# Static resources (not managed by the Play framework)
GET /my_external_assets/*file controllers.ExtAssets.at(file)
# Play's standard assets
GET /assets/*file controllers.Assets.at(path = "/public", file)
然后您需要定义资产控制器。以下是一个简单的示例:
public class ExtAssets extends Controller {
public Result at(String filePath) {
File file = new File(filePath);
return ok(file, true);
}
}
dist → Arbitrary files to be included in your projects distribution
Play 2.5
文件:路由
GET /external_resources/*file controllers.ImagesController.getImage(path="/home/project_name/external/images", file: String)
文件:ImagesController
package controllers;
import play.mvc.Controller;
import play.mvc.Result;
import java.io.File;
/**
* Created by js on 6/1/17.
*/
public class ImagesController extends Controller {
public Result getImage(String path, String image) {
System.out.println(path);
System.out.println(image);
image = image.replace("%20", " ");
File file = new File(path + image);
return ok(file);
}
}
cd ~/your/playframework/awesome/project/public
ln -s '/var/lib/funky-data-set-for-my-awesome-project/' funky-data
现在你可以在你的路由文件中使用类似这样的代码:
GET /funky/assets/*file controllers.Assets.at(path="/public/funky-data", file)