java 选择多个文件并使用 Jersey 上传它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25749472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:37:15  来源:igfitidea点击:

Selecting multiple files and uploading them using Jersey

javarestjerseymultipartform-datamultiple-file-upload

提问by Ujjwal Subedi

I need help with multiple file uploads using Jersey. I used the following code to upload a single file using Jersey.

我需要使用 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();
        }
    }


}

The above code works well with uploading a single file but when I add the multiple="multiple" attribute to the input type = file tag, I'm able to select multiple items in a single upload, it uploads the first selected item and the name of the last selected item. I'm not expecting the code to work cause it wasn't meant to handle multiple file uploads but there has to be a way around it, right?? Since, it's taking one file and the name of another one.

上面的代码适用于上传单个文件,但是当我将 multiple="multiple" 属性添加到 input type = file 标签时,我可以在一次上传中选择多个项目,它会上传第一个选定的项目和最后选择的项目的名称。我不希望代码能够工作,因为它不是为了处理多个文件上传,但必须有办法解决它,对吧?因为,它需要一个文件和另一个文件的名称。

I have looked multiple stackoverflow threads and googled a lot. I wouldn't have posted this if I had found the answer. I'm not looking for uploading multiple files using the types of code below:

我查看了多个 stackoverflow 线程并搜索了很多。如果我找到了答案,我就不会发布这个了。我不是在寻找使用以下代码类型上传多个文件:

<input type ="file" name="file">
<input type ="file" name="file">
<input type ="file" name="file2">

I don't want to upload multiple files individually. I want to be able to select multiple files at a time and all of them to be uploaded somewhere. My tag is like this:

我不想单独上传多个文件。我希望能够一次选择多个文件,并将它们全部上传到某个地方。我的标签是这样的:

<input type ="file" name="file" multiple="multiple">

Here's the entire HTML code.

这是整个 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>

These are the jars I've used http://i.stack.imgur.com/1tVT8.png

这些是我用过的罐子 http://i.stack.imgur.com/1tVT8.png

回答by u4152709

It works OK for me to use the 'FormDataMultiPart'.

我可以使用“FormDataMultiPart”。

Here is the Java code, the FormDataContentDisposition object(formParams) contains actual file content.

这是 Java 代码,FormDataContentDisposition 对象(formParams)包含实际的文件内容。

List<FormDataBodyPart> parts = formParams.getFields("file");
for (FormDataBodyPart part : parts) {
    FormDataContentDisposition file = part.getFormDataContentDisposition();
}

In the JS side, I use the FormData object and push several files with the same name:

在JS端,我使用FormData对象,推送几个同名文件:

for (var i = 0; i < files.length; i++)
    fd.append('file', files[i]);

Wish it will help

希望它会有所帮助

回答by dandeeee

This thing worked for me pretty well:

这件事对我很有效:

@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);
    }
}