Play 框架 - 如何写入公共资产 [英] Play framework - how to write to public assets
问题描述
尝试处理上传的图像,然后显示结果.以下是:https://github.com/playframework/play-scala-fileupload-example
Trying to process uploaded images and then display the result. Was following: https://github.com/playframework/play-scala-fileupload-example
相关片段:
private def handleFilePartAsFile: FilePartHandler[File] = {
case FileInfo(partName, filename, contentType) =>
val path: Path = Files.createTempFile("multipartBody", "tempFile")
val fileSink: Sink[ByteString, Future[IOResult]] = FileIO.toPath(path)
val accumulator: Accumulator[ByteString, IOResult] = Accumulator(fileSink)
accumulator.map {
case IOResult(count, status) =>
logger.info(s"count = $count, status = $status")
FilePart(partName, filename, contentType, path.toFile)
}
}
如何获取 /public/images
资产文件夹的绝对路径,将图像写入其中,而不是使用 Files.createTempFile
?
How can I get the absolute path of the /public/images
asset folder, to write the image to, instead of using Files.createTempFile
?
推荐答案
我想您会像在您提到的链接中的项目中一样使用 Play 2.6.在这种情况下,您可以从 4 个路径构建/public/images 文件夹中的文件路径:
I suppose you will use Play 2.6 like in the project from the link you mentioned. In that case you can construct path to file in /public/images folder from 4 paths:
- 已部署 Play 的绝对路径!申请
- 资产文件夹的基本(相对)路径
- /图片
- 文件名
在斯卡拉:
val path = env.rootPath + af.assetsBasePath + "/images/" + filename
对于第一和第二部分,您需要自动注入 Environment 和 AssetsFinder 对象,例如:
For 1st and 2nd parts you need to auto-inject Environment and AssetsFinder objects like:
@Singleton
class HomeController @Inject() (cc:MessagesControllerComponents,
af: AssetsFinder, env: Environment)
(implicit executionContext: ExecutionContext)
...
这篇关于Play 框架 - 如何写入公共资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!