apache commons movefile方法在windows中失败

时间:2013-11-23 06:17:22

标签: scala playframework apache-commons

我有一个播放应用程序。我正在使用play-resplay-scalr库在服务器端调整图像大小。 当我使用图像文件进行多部分表单提交时。在服务器端,我只能在Windows系统上获得以下异常,而不是在mac上。

堆栈跟踪

Caused by: java.io.IOException: Failed to delete original file 'E:\tmp\multipartBody677849053220396275asTemporaryFile' after copy to 'E:\work\projects\rismus\res\profile\268d\3a0d\268d3a0d9a36e7cc8b826b274de4aaa47fdc6591.jpg'
    at org.apache.commons.io.FileUtils.moveFile(FileUtils.java:2835) ~[commons-io.jar:2.4]
    at se.digiplant.res.api.Res$.put(Res.scala:81) ~[play-res_2.10-1.0.1.jar:1.0.1]
    at se.digiplant.res.api.Res$.put(Res.scala:117) ~[play-res_2.10-1.0.1.jar:1.0.1]
    at controllers.ImageUploader$$anonfun$upload$2$$anonfun$apply$1$$anonfun$apply$2$$anonfun$1.apply(ImageUploader.scala:37) ~[na:na]
    at controllers.ImageUploader$$anonfun$upload$2$$anonfun$apply$1$$anonfun$apply$2$$anonfun$1.apply(ImageUploader.scala:36) ~[na:na]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library.jar:na]
    at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
    at scala.collection.AbstractTraversable.map(Traversable.scala:105) ~[scala-library.jar:na]
    at controllers.ImageUploader$$anonfun$upload$2$$anonfun$apply$1$$anonfun$apply$2.apply(ImageUploader.scala:36) ~[na:na]
    at controllers.ImageUploader$$anonfun$upload$2$$anonfun$apply$1$$anonfun$apply$2.apply(ImageUploader.scala:26) ~[na:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24) ~[scala-library.jar:na]
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24) ~[scala-library.jar:na]
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:42) ~[akka-actor_2.10.jar:2.2.0]
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:386) ~[akka-actor_2.10.jar:2.2.0]
    ... 4 common frames omitted

如果需要更多信息来了解问题,请告诉我。

编辑:上传功能

def upload(accountId: Long): EssentialAction = Authenticated(
    account => Action.async(parse.multipartFormData) { request => Future {
        if (account.id != accountId) Results.Unauthorized
        else {

          if (request.body.files.filter(f => !checkFileType(f)).length > 0) {
            BadRequest("Unsupported file type")
          } else {

            val assets = request.body.files.map { file =>
              val assetId = Res.put(file, source, Seq.empty) //fails here
              Map("asset_id" -> assetId, "mime" -> file.contentType.get)
            }

            if (assets.length == 0) BadRequest("No Images uploaded")


            DB.withConnection { implicit c =>
              assets.foreach { asset =>
                  Accounts.updatePicture(accountId, asset("asset_id"))
              }
            }

            Ok(Json.toJson(assets))

          }
        }
      }
    },
    failedResult = Results.Unauthorized
  ) 

1 个答案:

答案 0 :(得分:0)

在Windows上,文件在打开时被锁定,无法删除。在Unix / Linux上,文件通常可以在打开时删除(除非涉及网络文件系统) - 文件仍然存在于磁盘上,仍然占用空间,直到它的最后一个文件描述符被关闭,但是从目录中分离出来,所以它是不可见。

所以问题是临时文件还没有关闭。