简体   繁体   English

如何在Play框架中上传文件并在目标位置复制相同文件

[英]How to upload file and copy the same file at destination in Play framework

I wanted to upload my file at one source and should copy the same file at another destination from that source in Play framework, below is my code and please let me know where I am doing wrong as it is giving error like: error: method move in class Files cannot be applied to given types;(The method move(Path, Path, CopyOption...) in the type Files is not applicable for the arguments (String, String, StandardCopyOption). 我想将文件上传到一个源,并应该在Play框架中从该源将文件复制到另一个目标,以下是我的代码,请告诉我我在哪里做错了,因为它给出了错误,例如:error:method move类中的File不能应用于给定的类型;(在Files类型中的move(Path,Path,CopyOption ...)方法不适用于参数(String,String,StandardCopyOption)。

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: HTML:

@form(action = routes.Application.upload, 'enctype -> "multipart/form-data") {
    <input type="file" name="file">
    <p>
        <input type="submit">
    </p>
   }

It is not working because java.nio.file.Files.move uses Path instead Strings: java.nio.file.Files.move( file.toPath() , newFile.toPath() , StandardCopyOption.ATOMIC_MOVE); 它不起作用,因为java.nio.file.Files.move使用Path代替了字符串:java.nio.file.Files.move( file.toPath()newFile.toPath() ,StandardCopyOption.ATOMIC_MOVE);

Other good alternative easy to read is org.apache.commons.io.FileUtils.moveFile(file, new File(path, fileName)); 其他易于阅读的替代方法是org.apache.commons.io.FileUtils.moveFile(file, new File(path, fileName)); this example to move files . 此示例移动文件

This code is not tested: 此代码未经测试:

Http.MultipartFormData.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

    File newFile = new File("D:/images/files/" + uploadedFile.getFilename());

    java.nio.file.Files.move( file.toPath(),newFile.toPath(), StandardCopyOption.ATOMIC_MOVE);

   // org.apache.commons.io.FileUtils.moveFile(file, new File(path, fileName));

    return ok("File is uploaded at source and copied the same file at destination");
}

Hope this may help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM