Play Framework - 上传文件后响应 JSON (multipartFormData)

Posted

技术标签:

【中文标题】Play Framework - 上传文件后响应 JSON (multipartFormData)【英文标题】:Play Framework - Respond with JSON after uploading a file (multipartFormData) 【发布时间】:2021-09-13 07:58:29 【问题描述】:

我正在使用此代码将图像上传到服务器上,我从这个链接play upload获得的图像

 def upload = Action(parse.multipartFormData)  request =>
    request.body
      .file("picture")
      .map  picture =>
        val dataParts = request.body.dataParts;

        val filename = Paths.get(picture.filename).getFileName
        val fileSize = picture.fileSize
        val contentType = picture.contentType

         val picturePaths =
           picture.ref.copyTo(
             Paths.get(
               s"/opt/docker/images/$filename"
             ),
             replace = true
           )
        if (dataParts.get("firstPoint") == None) 
          val pointlocation = new Point_LocationModel(
            dataParts.get("step").get(0),
            dataParts.get("backgroundTargetName").get(0),
            dataParts.get("x").get(0),
            dataParts.get("y").get(0),
            dataParts.get("z").get(0),
            dataParts.get("rotation").get(0),
            dataParts.get("note").get(0),
            dataParts.get("tag").get(0),
            dataParts.get("work_session_id").get(0),
            (picturePaths).toString
          )
          point_LocationRepository.create(pointlocation).map  data =>
            Created(Json.toJson(data._2))
          
         else 
          val jValuefirstPoint =
            Json.parse(dataParts.get("firstPoint").get(0)).as[PointModel]
          val jValuesecondPoint =
            Json.parse(dataParts.get("secondPoint").get(0)).as[PointModel]

          val pointlocation = new Point_LocationModel(
            dataParts.get("step").get(0),
            dataParts.get("backgroundTargetName").get(0),
            Some(jValuefirstPoint),
            Some(jValuesecondPoint),
            dataParts.get("rotation").get(0),
            dataParts.get("note").get(0),
            dataParts.get("tag").get(0),
            dataParts.get("work_session_id").get(0),
            (picturePaths).toString
          )
          point_LocationRepository.create(pointlocation).map  data =>
            logger.info(s"repoResponse:  $data");
            Created(Json.toJson(data._2))
          
        
        Ok(s"picturePaths $picturePaths")

      
      .getOrElse(Ok("Invalid Format"))
  

此代码运行良好,但在响应方面我想从存储库中获取响应。我如何等待存储库的响应来返回这个?

你能告诉我我该怎么做吗? 提前致谢。

【问题讨论】:

好吧,你尝试了什么?是什么阻碍了你?您已经拥有这两个信息(名称和大小),只需在响应中发送它即可。 您好,我已经针对我已实现的特定代码更改了问题。提前致谢。 【参考方案1】:

如果我们将您的代码简化为基本部分,您将拥有:

def upload = Action(parse.multipartFormData)  request =>
    request.body
      .file("picture")
      .map  picture =>
        if (someConditionA) 
          someBusinessLogicA().map  data =>
            Created(Json.toJson(data._2))
          
         else 
          someBusinessLogicB().map  data =>
            Created(Json.toJson(data._2))
          
        
        Ok(s"picturePaths $picturePaths")
      
      .getOrElse(Ok("Invalid Format"))

有2个问题:

您的if/else 的返回被Ok(s"picturePaths $picturePaths") 吞噬 假设您的业务逻辑返回 Future[_],您需要在任何地方“传播”未来

这样的事情应该可以工作:

def upload = Action.async(parse.multipartFormData)  request =>
    request.body
      .file("picture")
      .map  picture =>
        if (someConditionA) 
          someBusinessLogicA().map  data =>
            Created(Json.toJson(data._2))
          
         else 
          someBusinessLogicB().map  data =>
            Created(Json.toJson(data._2))
          
        
      
      .getOrElse(Future.successful(Ok("Invalid Format")))

注意getOrElse 中的Action.asyncFuture

【讨论】:

以上是关于Play Framework - 上传文件后响应 JSON (multipartFormData)的主要内容,如果未能解决你的问题,请参考以下文章

Play 2.x:使用 Iteratees 进行响应式文件上传

Play Framework Secure Module:登录JSON响应接口

使用 Play Framework for Java,如何从控制器返回 500 响应?

Play Framework 2.5 JavaAsync 抛出 CompletionException

为啥 Play Framework 战争包含重复的类文件?

Play framework 2.4 打包后无法加载图片(404 source not found)