【问题标题】:Multiple file upload in playframeworkplayframework中的多个文件上传
【发布时间】:2011-10-03 12:45:32
【问题描述】:

我在上传多个文件时遇到了一些问题。当我选择 x 个文件时,它成功通过,但第一个文件正在上传 x 次,其他文件根本没有上传。谁能指出我做错了什么?

表格:

#{form @Projects.uploadPictures(project.id), enctype:'multipart/form-data'}   

<p>
    <label>&{'title'}</label>
    <input type="text" name="title"/>
    <strong>(&{'addPicture.chooseTitle'})</strong>
</p>
<p>
    <label>&{'Pictures'}</label>
    <input type="file" multiple name="files" id="files"/>
</p>
<p>
    <input type="submit" value="&{'publish'}" />
</p>

#{/form}

处理文件:

public static void uploadPictures(long id, String title, List<Blob> files) {
    String error = "";        
    if(files != null && !title.trim().equals("")) {
        Project project = Project.findById(id);
        // Save uploaded files
        Picture picture;

        for(int i = 0; i<files.size(); i++) {
            if(files.get(i) != null) {
                System.out.println("i: "+i+"\nFiltype: "+files.get(i).type());
                if(files.get(i).type().equals("image/jpeg") || files.get(i).type().equals("image/png")) {
                    picture = new Picture(project, title+"_bilde_"+(i+1), files.get(i));
                    project.addPicture(picture);
                } else {
                    error += "Fil nummer "+(i+1)+" er av typen "+files.get(i).type()+" og ikke av typen .JPG eller .PNG og ble dermed ikke lagt til. \n";
                }
            } else {
                error = "Ingen filer funnet";
            }
        }
    } else {
        error = "Velg en tittel for bildene";
    }
    if(error.equals("")) {
        flash.success("Picture(s) added");
    } else {
        flash.error(error);
    }
    addPicture(id);
}

【问题讨论】:

    标签: java file-upload playframework


    【解决方案1】:

    如果有人感兴趣,就让它像这样工作:

    public static void uploadPictures(long id, String title, File fake) {
        List<Upload> files = (List<Upload>) request.args.get("__UPLOADS");
        if(files != null) {
            Project project = Project.findById(id);
            Picture picture;
            Blob image;
            InputStream inStream;
            for(Upload file: files) {
                if(file != null) {
                    try {
                        inStream = new java.io.FileInputStream(file.asFile());
                        image = new Blob();
                        image.set(inStream, new MimetypesFileTypeMap().getContentType(file.asFile()));
                        picture = new Picture(project, file.getFileName(), image);
                        project.addPicture(picture); // stores the picture
                    } catch (FileNotFoundException e) {
                        System.out.println(e.toString());
                    }
                }
            }
        }
        addPicture(id); //renders the image upload view
    }
    

    如果可能的话,我很乐意使用一组 Blob 对象而不是 request.args.get("__UPLOADS") 来获得一个可行的解决方案。

    【解决方案2】:

    因此您可以使用@As 将参数的处理绑定到特定的 Play TypeBinder

    所以有了这个:

    public static void chargedMultiUpload(@As(binder = FileArrayBinder.class) Object xxx) throws IOException{ ... }
    

    还有这个html

    <input type="file" multiple name="files" id="files"/>
    

    所以,你必须使用类似 File[] doo = (File[])xxx;

    【讨论】:

    • 您可以创建自己的活页夹,让控制器变得更干净
    【解决方案3】:

    &lt;input type="file" multiple name="files" id="files"/&gt; 不应该是:&lt;input type="file multiple" name="files" id="files"/&gt;

    其次,您实际上将图像保存在哪里?我认为你应该把它保存在你的循环中,你放project.addPicture(picture);,但实际上它看起来像在你的最后一行中将图像保存到系统中:addPicture(id); 这有点解释了为什么它保存相同的图像(最后一个或第一个(不确定如何解析))多次。

    【讨论】:

    • 感谢您的回答,但 没有呈现为多个文件上传。 project.addPicture(picture) 保存图像, addPicture(id) 只渲染视图以供图像上传。还有其他想法吗?
    • 绝对不是。 multiple is a separate attribute 而不是 type 值的一部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2019-01-29
    • 2016-08-10
    相关资源
    最近更新 更多