在playframework中多文件上传 [英] Multiple file upload in playframework
本文介绍了在playframework中多文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使多个文件上传工作时遇到一些问题。当我选择x文件时,它会成功,但是第一个文件上传x次,其他文件根本没有上传。任何人都能指出我做错了什么?
表格:
#{form @ Projects.uploadPictures(project .id),enctype:'multipart / form-data'}
< p>
< label>& {'title'}< / label>
< input type =textname =title/>
< strong>(& {'addPicture.chooseTitle'})< / strong>
< / p>
< p>
< label>& {'Pictures'}< / label>
< input type =filemultiple name =filesid =files/>
< / p>
< p>
< input type =submitvalue =& {'publish'}/>
< / p>
$ b $ {/ form}
处理档案:
$ p $
public static void uploadPictures(long id,String title,List< Blob> files){
String error =;
if(files!= null&&!title.trim()。equals()){
Project project = Project.findById(id);
//保存上传的文件
图片图片;
$ b $ for(int i = 0; i< files.size(); i ++){
if(files.get(i)!= null){
System.out .println(i:+ i +\\\
Filtype:+ files.get(i).type()); (file.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。\\\
;
}
} else {
error =Ingen filer funnet;
}
}
} else {
error =Velil 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<上载] GT; files =(List< Upload>)request.args.get(__ UPLOADS);
if(files!= null){
Project project = Project.findById(id);
照片图片;
Blob图片;
InputStream inStream;
for(上传文件: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); //存储图片
} catch(FileNotFoundException e){
System.out.println(e.toString());
}
}
}
}
addPicture(id); //呈现图片上传视图
}
很高兴能得到一个可行的解决方案一个Blob对象数组,如果可能的话,不必request.args.get(__ UPLOADS)。
I'm having some problems with getting multiple file upload to work. When I select x files, it goes through successfully, but the first file is being uploaded x times, and the others are not being uploaded at all. Anyone able to point out what I am doing wrong?
Form:
#{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}
Handling the files:
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);
}
解决方案
Got it to work like this if anyone is ever interested:
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
}
Would be happy to get a working solution with an array of Blob objects instead of having to request.args.get("__UPLOADS") if possible.
这篇关于在playframework中多文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文