玩!上载图像并使它们可用于/assets/images [英] Play! upload images and make them available in production at /assets/images
问题描述
来自文档:
打包应用程序时,该应用程序的所有资产(包括所有子项目)都聚集在target/my-first-app-1.0.0-assets.jar中的单个jar中.该jar包含在分发中,因此您的Play应用程序可以为他们提供服务.该jar也可以用于将资产部署到CDN或反向代理.
When you package your application, all assets for the application, including all sub projects, are aggregated into a single jar, in target/my-first-app-1.0.0-assets.jar. This jar is included in the distribution so that your Play application can serve them. This jar can also be used to deploy the assets to a CDN or reverse proxy.
我的应用程序中上传了一些图片,在开发过程中一切正常,但在生产中却出现了错误.
I got a few image uploads in my application, everything works fine in development but in production it gives me errors.
问题是,我需要public/images文件夹中的那些文件才能在页面上显示它们,但在生产环境中不起作用.
The thing is, I need those files in the public/images folder in order to display them on the page but it does not work in production.
我如何让用户上传图像,并使它们在/public/images中可用?
How can I let the user upload images and also make them available at /public/images ?
我是否必须创建另一个Assets路径或类似的路径?
Do I have to create another Assets path or something like that ?
注意:之所以没有发布代码,是因为这只是文档中的标准上传方法.
note: Didn't post code because it's just a standard upload method from the docs.
推荐答案
我做了什么:
我知道这可能只是一个愚蠢的解决方法.
I'm aware of the fact that this might be just a stupid workaround.
GET /images/:name controllers.ImageController.show(name: String)
我创建了一个简单的操作,用于提供类似以下内容的图像:
I created a simple action that serves the images like:
def show(name: String) = Action { implicit r =>
val rootPath = Play.application.path
val path = FileSystems.getDefault().getPath("/" + rootPath + "/public/images/");
Logger.info("serving image: " + path.toString + "/" + name )
Ok.sendFile(new java.io.File(path.toString + "/" + name))
}
基本上,整个操作在开发中将失败,但在生产中会起作用.
Basically this whole operation will fail in development but it works in production.
部署后,我手动创建了文件夹,例如
After deployment, I created the folders manually e.g
your_root/target/universal/stage/public/images
现在所有图像都可以在以下位置获得:
Now all the images are available at:
/images/name_of_the_image
注意:播放版本= 2.4.x
note: Play version = 2.4.x
这篇关于玩!上载图像并使它们可用于/assets/images的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!