2
我想在一個源文件上傳我的文件,並且應該在Play框架中將該文件複製到另一個目標文件中,下面是我的代碼,請讓我知道我在做什麼錯誤,因爲它給錯誤如:錯誤:方法在類中移動文件不能應用於給定的類型;(類型文件中的方法move(Path,Path,CopyOption ...)不適用於參數(字符串,字符串,StandardCopyOption)如何在Play框架中上傳文件並複製目的地中的相同文件
public static Result upload()
{
MultipartFormData body = request().body().asMultipartFormData();
FilePart uploadedFile = body.getFile("file");
if (uploadedFile != null) {
String fileName = uploadedFile.getFilename();
String contentType = uploadedFile.getContentType();
File file = uploadedFile.getFile();
file.renameTo(new File("D:/images/" +fileName));// it can be source
java.nio.file.Files.move("D:/images/", "D:/images/files/", StandardCopyOption.ATOMIC_MOVE);
return ok("File is uploaded at source and copied the same file at destination");
}
else
{
return badRequest("No file is uploaded.");
}
}
HTML:
@form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {
<input type="file" name="file">
<p>
<input type="submit">
</p>
}
感謝您的回覆,我已經嘗試了這一點,但我得到的錯誤:意外的異常:RuntimeException的:沒有HTTP上下文(來自:java.nio.file.Files.move(file.toPath(),newFile.toPath(),StandardCopyOption.ATOMIC_MOVE);) – Dhana