PlayFramework 如何在上传时获得正确的文件路径
PlayFramework how can I get correct file path on upload
我有 play framework 版本 2.46,我正在将文件上传到我的 amazon S3 帐户,但是我一直在获取 tmp 文件的路径。这是我的代码
def upload = Action (parse.multipartFormData) { implicit request =>
request.body.file("file").map { file =>
val credentials: AWSCredentials = new BasicAWSCredentials("Key", "Secret-key")
val s3client: AmazonS3 = new AmazonS3Client(credentials)
val bucketName: String = "myfolder"
val myfile:File= new File(file.filename)
s3client.putObject(new PutObjectRequest(bucketName,file.filename,new File(myfile.getAbsolutePath)))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file")
}
}
给我问题的部分是 myfile.getAbsolutePath 部分,它返回找不到的错误文件。我确切地知道为什么但不知道如何修复它,例如如果我在 C:\mypictures\Users\bus.jpg 有一个文件然后 myfile.getAbsolutePath 会把它变成 C:\mypictures\Calc\bus.jpg 。我的 PlayFramework 应用程序名为 Calc,当然该位置不存在该图片。
- 作品:新文件("C:\mypictures\Users\bus.jpg") = C:\mypictures\Users\bus.jpg
- 不起作用新文件(myfile.getAbsolutePath)= C:\mypictures\Calc\bus.jpg
如何解决此问题以便上传正确的文件位置?我被困在这几个小时
您似乎正在寻找从您计算机上的该位置提取文件的网络应用程序,而不是它在 HTTP 请求中接收文件。
val myfile:File= new File(file.filename)
将不起作用,因为您正在使用上传文件的名称(而不是路径),并使用该名称和当前工作目录创建一个 File
- 这个然后在你用它获取绝对目录时反映出来。
也不需要它,因为您已经有 TemporaryFile
。来自 documentation:
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
import java.io.File
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(new File(s"/tmp/picture/$filename"))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file")
}
}
因此,您可以使用该临时文件为您提供所需的一切。然后你的代码变成
def upload = Action (parse.multipartFormData) { implicit request =>
request.body.file("file").map { file =>
val credentials: AWSCredentials = new BasicAWSCredentials("Key", "Secret-key")
val s3client: AmazonS3 = new AmazonS3Client(credentials)
val bucketName: String = "myfolder"
s3client.putObject(new PutObjectRequest(bucketName,
file.filename,
file.ref.file))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file")
}
}
我有 play framework 版本 2.46,我正在将文件上传到我的 amazon S3 帐户,但是我一直在获取 tmp 文件的路径。这是我的代码
def upload = Action (parse.multipartFormData) { implicit request =>
request.body.file("file").map { file =>
val credentials: AWSCredentials = new BasicAWSCredentials("Key", "Secret-key")
val s3client: AmazonS3 = new AmazonS3Client(credentials)
val bucketName: String = "myfolder"
val myfile:File= new File(file.filename)
s3client.putObject(new PutObjectRequest(bucketName,file.filename,new File(myfile.getAbsolutePath)))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file")
}
}
给我问题的部分是 myfile.getAbsolutePath 部分,它返回找不到的错误文件。我确切地知道为什么但不知道如何修复它,例如如果我在 C:\mypictures\Users\bus.jpg 有一个文件然后 myfile.getAbsolutePath 会把它变成 C:\mypictures\Calc\bus.jpg 。我的 PlayFramework 应用程序名为 Calc,当然该位置不存在该图片。
- 作品:新文件("C:\mypictures\Users\bus.jpg") = C:\mypictures\Users\bus.jpg
- 不起作用新文件(myfile.getAbsolutePath)= C:\mypictures\Calc\bus.jpg
如何解决此问题以便上传正确的文件位置?我被困在这几个小时
您似乎正在寻找从您计算机上的该位置提取文件的网络应用程序,而不是它在 HTTP 请求中接收文件。
val myfile:File= new File(file.filename)
将不起作用,因为您正在使用上传文件的名称(而不是路径),并使用该名称和当前工作目录创建一个 File
- 这个然后在你用它获取绝对目录时反映出来。
也不需要它,因为您已经有 TemporaryFile
。来自 documentation:
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
import java.io.File
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(new File(s"/tmp/picture/$filename"))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file")
}
}
因此,您可以使用该临时文件为您提供所需的一切。然后你的代码变成
def upload = Action (parse.multipartFormData) { implicit request =>
request.body.file("file").map { file =>
val credentials: AWSCredentials = new BasicAWSCredentials("Key", "Secret-key")
val s3client: AmazonS3 = new AmazonS3Client(credentials)
val bucketName: String = "myfolder"
s3client.putObject(new PutObjectRequest(bucketName,
file.filename,
file.ref.file))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.Application.index).flashing(
"error" -> "Missing file")
}
}