`
sillycat
  • 浏览: 2489828 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Playframework and FileUpload in RESTful

 
阅读更多
Playframework and FileUpload in RESTful

File Upload in RESTful API right now, there are only 3 options till now.
1. Base64 encode the file, at the expense of increasing the data size by around 33%.
2. Send the file first in a multipart/form-data POST, and return and ID to the client. The client then sends the metadata with the ID. The server re-associates the file and the metadata.
3. Send the metadata first, and return an ID to the client. The client then sends the file with the ID, and the server re-associates the file and the metadata.

I may choose #2 or #3. Looking at play framework Uploading Files Chapter.

1. Playframework File Upload
I will use “multipart/form-data” encoding to make the form support mix standard form data with file attachment data.

Logging Level
ch.qos.logback.classic.Level     TRACE, DEBUG, INFO, WARN and ERROR
https://www.playframework.com/documentation/2.4.3/SettingsLogger
https://www.playframework.com/documentation/2.4.3/ScalaLogging

  def upload = Action(parse.multipartFormData) { request =>
    request.body.file("file").map { file =>
      import java.io.File
      val filename = file.filename
      val contentType = file.contentType
      Ok("Resume there.")
    }.getOrElse {
      BadRequest(Json.obj("status" -> "OK", "message" -> "No files in the attachment."))
    }
  }

I was originally thinking that I may separate it into 2 steps. 1 - uploading file, 2 - parse file. But after think about this again. I may change that to 1 single step.

2. Directly Upload the File and Other Params
route configuration to upload the file content

# Resume Routes
POST    /api/v1/resume/parseFile        controllers.ResumeController.parseFile()
POST    /api/v1/resume/parseContent     controllers.ResumeController.parseContent()

The logging configuration
<configuration>

    <conversionRule conversionWord="coloredLevel" converterClass="play.api.Logger$ColoredLevel" />

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logs/application.log</file>
        <encoder>
            <pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%coloredLevel %logger{15} - %message%n%xException{10}</pattern>
        </encoder>
    </appender>

    <appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="FILE" />
    </appender>

    <appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="STDOUT" />
    </appender>

    <logger name="play" level="INFO" />
    <logger name="application" level="DEBUG" />

    <!-- Off these ones as they are annoying, and anyway we manage configuration ourself -->
    <logger name="com.avaje.ebean.config.PropertyMapLoader" level="OFF" />
    <logger name="com.avaje.ebeaninternal.server.core.XmlConfigLoader" level="OFF" />
    <logger name="com.avaje.ebeaninternal.server.lib.BackgroundThread" level="OFF" />
    <logger name="com.gargoylesoftware.htmlunit.javascript" level="OFF" />
    <logger name="controllers.ResumeController" level="DEBUG" />

    <root level="WARN">
        <appender-ref ref="ASYNCFILE" />
        <appender-ref ref="ASYNCSTDOUT" />
    </root>

</configuration>

Implementation Class to Deal with File

import java.io.FileInputStream

import com.wordnik.swagger.annotations._
import utils.IncludeLogger

import play.api.libs.json.{JsError, Json}
import play.api.mvc.{BodyParsers, Action, Controller}

…snip...
  implicit val resumeWrites = Json.writes[Resume]
  implicit val resumeReads = Json.reads[Resume]

  @ApiOperation(value = "parseFile",
    notes = "Parse the Resume File",
    response = classOf[Resume],
    httpMethod = "POST",
    produces = "json",
    position = 1)
  @ApiImplicitParams(Array(
    new ApiImplicitParam(
      name="body",
      value = "Resume File",
      required = true,
      dataType = "file",
      paramType = "body")))
  @ApiResponses(Array(new ApiResponse(code = 200, message = "Successful Parse Resume.", response = classOf[Resume]) ))
  def parseFile = Action(parse.multipartFormData) { request =>
    logger.debug("The request is here.")
    val resume = request.body.files.map { file =>
      val filename = file.filename
      logger.debug("file: " + filename + " " + file.contentType)
      val inputStream = new FileInputStream(file.ref.file)
      ResumeUtil.parseInputStream(inputStream)
    }
    Ok(Json.toJson(resume))
  }

Command to Play with Large Memory
>sbt clean update compile dist
>bin/classifier-play -Dconfig.file=conf/application.conf -Dhttp.port=8003 -Dhttp.address=0.0.0.0 -J-Xms1024M -J-Xmx8g -J-server

References:
http://stackoverflow.com/questions/3938569/how-do-i-upload-a-file-with-metadata-using-a-rest-web-service
http://stackoverflow.com/questions/4083702/posting-a-file-and-data-to-restful-webservice-as-json

File Upload for Scala
https://www.playframework.com/documentation/2.4.3/ScalaFileUpload
http://stackoverflow.com/questions/9452375/how-to-get-the-upload-file-with-other-inputs-in-play2
分享到:
评论

相关推荐

    restful restful所需要的jar包

    * Integration with Apache FileUpload to support multi-part forms and easily handle large file uploads from browsers * Transformer filter to easily apply XSLT stylesheets on XML representations. It ...

    Apache Commons FileUpload 1.3.2 released

    o FILEUPLOAD-272: Performance Improvement in MultipartStream Details of all changes in 1.3.2 can be found in the changelog: http://commons.apache.org/proper/commons-fileupload/changes-report.html ...

    开发工具 commons-fileupload-1.3.2

    开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2开发工具 commons-fileupload-1.3.2...

    play-java-fileupload-example.zip_java_play_rately7

    Play java fileupload example

    commons-fileupload-1.2.2

    commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-fileupload-1.2.2commons-...

    commons-fileupload-1.4-API文档-中文版.zip

    赠送jar包:commons-fileupload-1.4.jar; 赠送原API文档:commons-fileupload-1.4-javadoc.jar; 赠送源代码:commons-fileupload-1.4-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.4.pom; 包含...

    ASP.NET.FileUpload控件

    FileUpload控件的基本用法 .

    commons-fileupload-1.3.3-API文档-中文版.zip

    赠送jar包:commons-fileupload-1.3.3.jar; 赠送原API文档:commons-fileupload-1.3.3-javadoc.jar; 赠送源代码:commons-fileupload-1.3.3-sources.jar; 赠送Maven依赖信息文件:commons-fileupload-1.3.3.pom;...

    fileupload

    &lt;jsp:useBean id="myUpload" scope="page" class="fileUpload.upBean" /&gt; //初始化工作 myUpload.initialize(pageContext); //设定允许的文件后缀名 //myUpload.setAllowedExtList("gif,jpg"); //设定允许...

    commons commons-fileupload历史版本jar包集合

    commons commons-fileupload历史版本jar包集合,包括src源码 附件列表: commons-fileupload-1.0.zip commons-fileupload-1.1.1.zip commons-fileupload-1.1.zip commons-fileupload-1.2.1-bin.zip commons-...

    FileUpload笔记

    FileUpload笔记FileUpload笔记FileUpload笔记FileUpload笔记FileUpload笔记FileUpload笔记

    FileUpload

    文件上传 文件下载 FileUpload

    commons-fileupload commons-fileupload-1.2.1-bin

    commons-fileupload-1.2.1-bin commons-fileupload 文件上传 commons-fileupload-1.2.1-bin commons-fileupload 文件上传 jar包 及相关文档

    FileUpload美化CSS

    FileUpload 自带样式实在难看。 试了很多种方法,最终用了简单的css,简单美化了下。 其它思路:通过图片的click事件,触发FileUpload的click事件。但存在兼容性问题。并不能完全兼容所以浏览器。

    Commons FileUpload 1.4 + IO 2.2

    Java借助commons-fileupload组件实现文件上传所需包 Apache Commons FileUpload 1.4 (requires Java 1.6 or later) Note: This version of Commons FileUpload also requires Commons IO 2.2.

    commons-fileupload-1.4-bin.zip

    fileupload-1.4

    FileUpload组件使用方法

    FileUpload组件使用方法, FileUpload组件使用方法, FileUpload组件使用方法,

    commons-fileupload-1.3.3

    commons-fileupload-1.2.1.jar包;commons-lang-2.3.jar,在使用组件实现文件上传时候要注意前后台的一致性,注意在后台定义File文件对象名要与jsp页面中的file标签name名字一致,前台form标签属性一定要设置为...

    commons-fileupload及源码

    commons-fileupload apache开源项目,实现文件上传下载功能 ssh上传时必配

Global site tag (gtag.js) - Google Analytics