在playframework中上传多个文件

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

形成:

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

(&{'addPicture.chooseTitle'})

#{/form}

处理文件:

 public static void uploadPictures(long id, String title, List 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); } 

如果有人对此感兴趣,可以这样工作:

 public static void uploadPictures(long id, String title, File fake) { List files = (List) 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”)。

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

所以这个:

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

而这个HTML

  

所以,你必须使用File [] doo =(File [])xxx之类的东西进行转换;

应该不是:

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