在播放框架中上传文件时出现问题

problem when upload file in play framework

我试过上传一个文件,把文件放在"public / images"目录下,成功了,但是我上传的文件大小为零,肯定打不开

    public Result upload() throws IOException {
    Http.MultipartFormData<File> requestBody = request().body().asMultipartFormData();
    Http.MultipartFormData.FilePart<File> profile_pic = requestBody.getFile("profile_pic");
    String dirPath = "public/images/";      

    if(profile_pic != null) {
        String name = profile_pic.getFilename();
        File file = profile_pic.getFile();
        System.out.println(file);


    File theDir = new File(dirPath);
    if (!theDir.exists()) {
        boolean result = false;

        try {
            theDir.mkdirs();
            result = true;
        } catch (SecurityException se) {
            // handle it
        }
        if (result) {
            System.out.println("DIR created");
        }
    }


    try {

    File filex = new File(dirPath + name.toLowerCase());
    filex.createNewFile();

    file.renameTo(filex);


    }catch (Exception e) {
        // TODO: handle exception
    }
    return ok("File uploaded ");
    }else {
    return badRequest("Eroor");
    }
}

this is my file after upload

我认为创建新文件并将旧文件重命名为该名称可能会造成麻烦。我建议使用 move 方法,请参阅文档 here.

您的 System.out.println(file); 打印的文件看起来像普通的 png 文件吗?