Ujj*_*edi 3 java rest multipartform-data jersey multiple-file-upload
我需要使用Jersey的多个文件上传帮助.我使用以下代码使用Jersey上传单个文件.
package my.first.rest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
@Path("uploadfile")
public class Upload {
String location;
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadfile(@FormDataParam("file") InputStream is, @FormDataParam("file") FormDataContentDisposition filedetail){
saveToDisk(is,filedetail);
return "File Uploaded Succesfully_"+location;
}
private void saveToDisk(InputStream is1,
FormDataContentDisposition filedetail) {
// TODO Auto-generated method stub
location = "E://upload/"+filedetail.getFileName();
try{
OutputStream out = new FileOutputStream(new File(location));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream (new File(location));
while((read = is1.read(bytes)) != -1){
out.write(bytes,0,read);
}
out.flush();
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码适用于上传单个文件,但是当我将multiple ="multiple"属性添加到input type = file标签时,我可以在单个上传中选择多个项目,它会上传第一个选定的项目和最后一个选定项目的名称.我不希望代码工作,因为它不是为了处理多个文件上传,但必须有一个解决方法,对吧?因为,它取一个文件和另一个文件的名称.
我看了很多stackoverflow线程并且google了很多.如果我找到了答案,我就不会发布这个.我不是要使用下面的代码类型上传多个文件:
<input type ="file" name="file">
<input type ="file" name="file">
<input type ="file" name="file2">
Run Code Online (Sandbox Code Playgroud)
我不想单独上传多个文件.我希望能够一次选择多个文件,并且所有文件都可以在某个地方上传.我的标签是这样的:
<input type ="file" name="file" multiple="multiple">
Run Code Online (Sandbox Code Playgroud)
这是整个HTML代码.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action ="http://localhost:8080/fuseframework/uploadfile/upload" method="post" enctype="multipart/form-data">
file:
<br>
<input type ="file" name="file" multiple="multiple">
<input type="submit" value="send">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这些是我用过的罐子 http://i.stack.imgur.com/1tVT8.png
小智 7
我可以使用'FormDataMultiPart'.
这是Java代码,FormDataContentDisposition对象(formParams)包含实际的文件内容.
List<FormDataBodyPart> parts = formParams.getFields("file");
for (FormDataBodyPart part : parts) {
FormDataContentDisposition file = part.getFormDataContentDisposition();
}
Run Code Online (Sandbox Code Playgroud)
在JS方面,我使用FormData对象并推送具有相同名称的多个文件:
for (var i = 0; i < files.length; i++)
fd.append('file', files[i]);
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助
小智 6
这件事对我来说很好用:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadMultiple(@FormDataParam("file") FormDataBodyPart body){
for(BodyPart part : body.getParent().getBodyParts()){
InputStream is = part.getEntityAs(InputStream.class);
ContentDisposition meta = part.getContentDisposition();
doUpload(is, meta);
}
}
Run Code Online (Sandbox Code Playgroud)