Play 框架 - 如何写入公共资产 [英] Play framework - how to write to public assets

查看:50
本文介绍了Play 框架 - 如何写入公共资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试处理上传的图像,然后显示结果.以下是: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:

  1. 已部署 Play 的绝对路径!申请
  2. 资产文件夹的基本(相对)路径
  3. /图片
  4. 文件名

在斯卡拉:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆