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

问题描述 投票:0回答:1

我尝试上传文件,并将该文件放置在“ 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

java file-upload playframework image-upload
1个回答
0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.