4

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


}

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

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

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

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

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

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

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

4

3 回答 3

10

这件事对我很有效:

@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);
    }
}
于 2015-12-06T20:32:44.940 回答
8

我可以使用“FormDataMultiPart”。

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

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

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

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

希望它会有所帮助

于 2014-10-17T07:29:04.960 回答
0

此答案还允许您在类型为 FormDataBodyPart 时将“文件”作为 @FormDataParam 添加到 @BeanParam 类。FormDataMultiPart 只会在请求中作为参数构建,当被子类继承时不会构建。

于 2021-07-22T16:54:44.940 回答