播放框架文件上传没有此类文件异常

时间:2017-01-29 05:11:14

标签: java scala ubuntu intellij-idea playframework-2.0

我正在尝试使用play framework scala实现文件上传。这段代码在开发(windows)中工作正常,但在部署时失败(Ubuntu)。

     def upload = Action(parse.multipartFormData) { request =>
      request.body.file("torrentFile").map { picture =>
      import java.io.File
      val filename = picture.filename
      val contentType = picture.contentType
      println(filename)
      println(contentType)
      picture.ref.moveTo(new File("Files/ppp.rdf"))
      Ok("File uploaded")
    }.getOrElse {
      Redirect(routes.Application.index()).flashing(
        "error" -> "Missing file")
    }
}

这是我尝试上传文件时遇到的错误

    play.api.UnexpectedException: Unexpected exception[NoSuchFileException: /tmp/playtemp5801072751187159177/multipartBody5640251320440161134asTemporaryFile -> Files/ppp.rdf]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:289) ~[com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220) ~[com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160) [com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at play.core.j.JavaGlobalSettingsAdapter.play$core$j$JavaGlobalSettingsAdapter$$super$onError(JavaGlobalSettingsAdapter.scala:35) [com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at play.core.j.JavaGlobalSettingsAdapter$$anonfun$onError$2.apply(JavaGlobalSettingsAdapter.scala:35) [com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at play.core.j.JavaGlobalSettingsAdapter$$anonfun$onError$2.apply(JavaGlobalSettingsAdapter.scala:35) [com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at scala.Option.getOrElse(Option.scala:121) [org.scala-lang.scala-library-2.11.7.jar:na]
        at play.core.j.JavaGlobalSettingsAdapter.onError(JavaGlobalSettingsAdapter.scala:35) [com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100) [com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100) [com.typesafe.play.play-netty-server_2.11-2.5.10.jar:2.5.10]
Caused by: java.nio.file.NoSuchFileException: /tmp/playtemp5801072751187159177/multipartBody5640251320440161134asTemporaryFile -> Files/ppp.rdf
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) ~[na:1.8.0_111]
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[na:1.8.0_111]
        at sun.nio.fs.UnixCopyFile.move(UnixCopyFile.java:457) ~[na:1.8.0_111]
        at sun.nio.fs.UnixFileSystemProvider.move(UnixFileSystemProvider.java:262) ~[na:1.8.0_111]
        at java.nio.file.Files.move(Files.java:1395) ~[na:1.8.0_111]
        at play.api.libs.Files$TemporaryFile.moveTo(Files.scala:102) ~[com.typesafe.play.play_2.11-2.5.10.jar:2.5.10]
        at controllers.Upload$$anonfun$upload$1$$anonfun$apply$1.apply(Upload.scala:74) ~[untitled10.untitled10-1.0-sans-externalized.jar:na]
        at controllers.Upload$$anonfun$upload$1$$anonfun$apply$1.apply(Upload.scala:68) ~[untitled10.untitled10-1.0-sans-externalized.jar:na]
        at scala.Option.map(Option.scala:146) [org.scala-lang.scala-library-2.11.7.jar:na]
        at controllers.Upload$$anonfun$upload$1.apply(Upload.scala:68) ~[untitled10.untitled10-1.0-sans-externalized.jar:na]

应用程序在运行时创建一些文件和文件夹,这当前发生,所以我确定它不是目录中的Linux文件权限。 当我转到/tmp目录时,我看不到播放临时文件。 我正在使用intellij中的play 2.5。

1 个答案:

答案 0 :(得分:1)

首先,尝试提供完整路径而不是Files,例如/var/webapps/your-app/files

root 临时(使用sudo)运行应用,并确保磁盘上存在Files(/ var / webapps / your-app / files)文件夹

如果应用程序工作为root,则表明权限存在问题。确保运行该应用程序的用户具有对/tmp的读/写权限以及对Files的写访问权限。