gpt4 book ai didi

java - Play Framework 文件上传无此类文件异常

转载 作者:行者123 更新时间:2023-12-04 18:30:02 27 4
gpt4 key购买 nike

我正在尝试使用 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目录我没有看到 Play 临时文件。
我正在使用 intellij 中的 play 2.5。

最佳答案

首先,尝试给出完整路径而不是Files ,如 /var/webapps/your-app/files .

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

如果应用程序以 root 运行它表明权限有问题。确保运行应用程序的用户对 /tmp 具有读/写权限和对 Files 的写入权限.

关于java - Play Framework 文件上传无此类文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41917834/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com