我希望有一個人可以幫助我。我使用以下代碼片段將影像上傳到我的服務器(這是從這里復制的:https : //www.playframework.com/documentation/2.8.x/ScalaFileUpload
```def upload = Action(parse.multipartFormData) { request =>
request.body
.file("file")
.map { picture =>
val filename = Paths.get(picture.filename).getFileName
val fileSize = picture.fileSize
val contentType = picture.contentType
// Test Print
print("content Type: " contentType)
print("filename: " filename)
print("filesize: " fileSize)
picture.ref.copyTo(Paths.get(s"/tmp/picture/$filename"), replace = true)
Ok("File uploaded")
}
.getOrElse {
null //TODO: changeme
}
}```
我用我的列印方法檢查了檔案名、檔案大小和內容型別,它似乎正在作業。
但我總是得到以下例外:
java.lang.ClassCastException: xsbti.BasicVirtualFileRef cannot be cast to java.io.File
at play.sbt.run.PlayReload$.$anonfun$sourceMap$1(PlayReload.scala:70)
at scala.collection.MapLike$MappedValues.get(MapLike.scala:261)
at play.runsupport.Reloader.$anonfun$findSource$1(Reloader.scala:552)
at scala.Option.flatMap(Option.scala:271)
at play.runsupport.Reloader.findSource(Reloader.scala:551)
at play.core.server.DevServerStart$$anon$1$$anon$2.sourceOf(DevServerStart.scala:174)
at play.core.SourceMapper.$anonfun$sourceFor$1(ApplicationProvider.scala:22)
at play.core.SourceMapper.$anonfun$sourceFor$1$adapted(ApplicationProvider.scala:22)
at scala.collection.IndexedSeqOptimized.$anonfun$find$1(IndexedSeqOptimized.scala:53)
at scala.collection.IndexedSeqOptimized.$anonfun$find$1$adapted(IndexedSeqOptimized.scala:53)
一些硬道理:
- 前端:Vue.js 2.6.12
- 后端:Play!Framework with Scala (2.12.8) 和 sbt 版本 4.0.0
- 這是我在 vue 中的輸入框:
<input type="file" ref="file" @change="onSelect" />
我真的希望有人可以幫助我:)。如果您需要更多資訊來幫助,請詢問我:)
謝謝!
uj5u.com熱心網友回復:
根據此執行緒,Play 可能會在某些 SBT 版本上出現問題,反之亦然。
此解決方法可能對您有用。
Play 2.8.8也修復了此問題,因此請嘗試更新到最新版本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/344795.html
