playframework - 文件上传

接着上一篇的play framework 数据库连接
、作为一个web项目、文件上传是必不可少的一个功能。

使用教程

jdbc配置

upload {
  path = "/Users/lake/dounine/github/public"
  prefix = "upload/" # or ""
  allow.extension = [".jpg", ".png", ".gif"]
  domain = "http://localhost:9000/assets"
}

控制器注入使用

@Singleton
class FileController @Inject()(ws: WSClient, configuration: Configuration, @NamedDatabase("default") db: Database, cache: CacheApi, cc: ControllerComponents)(implicit assetsFinder: AssetsFinder)
  extends AbstractController(cc) {

  private val logger = org.slf4j.LoggerFactory.getLogger(classOf[FileController])
  private val uploadPath = configuration.underlying.getString("upload.path")
  private val uploadPrefix = configuration.underlying.getString("upload.prefix")
  private val allowUploadExtension = configuration.underlying.getStringList("upload.allow.extension").toArray.map(_.toString)
  private val domain = configuration.underlying.getString("upload.domain")

  def upload = Action {
    request =>
      val uploadFiles = mutable.Set[JsValueWrapper]()
      val body = request.body.asMultipartFormData
      if (body.isDefined) {
        val files = body.get.files
        files.foreach(file => {
          val fileName = file.filename
          val extension = FilenameUtils.getExtension(fileName)
          if (extension != null && allowUploadExtension.contains(s".$extension")) {
            val toFileName = s"${UUID.randomUUID().toString.replace("-", "")}.$extension"
            val postPath = s"${LocalDate.now()}/$toFileName"
            val fullPath = s"$uploadPath/$uploadPrefix$postPath"
            val accessPath = s"$domain/$uploadPrefix$postPath"
            new File(s"$uploadPath/$uploadPrefix${LocalDate.now()}").mkdirs()
            val toFile = new File(fullPath)
            file.ref.copyTo(toFile, replace = false)
            uploadFiles += Json.obj("url" -> accessPath)
          }
        })
        TimeUnit.SECONDS.sleep(1)
        Ok(Json.obj("data" -> Json.arr(uploadFiles.toSeq: _*)))
      } else {
        BadRequest
      }
  }

}

使用postman上传文件测试

访问返回的地扯

最后

play framework真的很好用、添加代码动态加载。真的很棒。
下一篇文章继续介绍在playframework中如何处理跨域CORS。

MVC应用程序模型 - 7 - app/controllers - 8 - app/models - 8 - app/views - 8 - 请求生命周期 - 8 - 标准应用程序布局layout - 9 - app目录 - 9 - public目录 - 10 - conf目录 - 10 - lib目录 - 11 - 开发生命周期 - 11 - 连接到java调试器 - 12 - 类增强Enhancement - 13 - 02.HTTP路由 - 13 - 关于REST - 14 - routes文件语法 - 14 - HTTP方法 - 15 - URI范示 Pattern - 15 - Java调用定义 - 17 - 把404当作action来用 - 17 - 指派静态参数 - 17 - 变量和脚本 - 18 - 路由优先级 - 18 - 服务器静态资源 - 18 - staticDir: mapping - 18 - staticFile: mapping - 19 - URL 编码 - 19 - 反转路由:用于生成某些URL - 19 - 设置内容风格(CSS) - 20 - HTTP 内容协商 negotiation - 21 - 从http headers开始设置内容类型 - 21 - 定制格式 - 22 - 03.控制器 - 23 - 控制器概览 - 23 - 获取http参数 - 24 - 使用params map - 25 - 还可以从action方法签名实现转换 - 25 - 高级HTTP Java绑定 - 26 - 简单类型 - 26 - Date类型 - 26 - Calendar日历 - 27 - File - 27 - 支持类型的数组或集合 - 28 - POJO对象绑定 - 29 - JPA 对象绑定 - 30 - 定制绑定 - 30 - @play.data.binding.As - 30 - @play.data.binding.NoBinding - 31 - play.data.binding.TypeBinder - 31 - @play.data.binding.Global - 32 - 结果类型 - 32 - 返回一些文本类型的内容 - 33 - 返回一个JSON字符串 - 33 - 返回一个XML字符串F - 34 - 返回二进制内容 - 34 - 作为附件下载文件 - 34 - 执行一个模板 - 35 - 跳转到其他URL - 36 - Action链 - 36 - 定制web编码 - 37 - 拦截器 - 38 - @Before - 38 - @After - 39 - @Catch - 40 - @Finally - 41 - 控制器继承 - 42 - 使用@With注释添加更多的拦截器 - 42 - Because Java does not allow multiple inheritance, it can be very limiting to rely on the Controller hierarchy to apply interceptors. But you can define some interceptors in a totally different class, and link them with any controller using the @With annotation.由于java不允许多继承,通过控制器继承特点来应用拦截器就受到极大的限制。但是我们可以在一个完全不同的类里定义一些拦截器,然后在任何控制器里使用@With注释来链接他们。 - 42 - Session和Flash作用域 - 42 - 04.模板引擎 - 43 - 模板语法 - 43 - Expressions: ${…} - 44 - Template decorators : #{extends /} and #{doLayout /} - 44 - Tags: #{tagName /} - 45 - Actions: @{…} or @@{…} - 46 - Messages: &{…} - 46 - Comment: *{…}* - 46 - Scripts: %{…}% - 46 - Template inheritance继承 - 47 - 定制模板标签 - 48 - 检索tag参数 - 48 - 调用标签体 - 48 - 格式化特定标签 - 49 - 定制java标签 - 49 - 标签命名空间 - 50 - 在模板里的Java对象扩展 - 51 - 创建定制扩展 - 52 - 模板里可以使用的保留对象 - 52 - 05.用play验证http数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值