【问题标题】:Selecting multiple files and uploading them using Jersey选择多个文件并使用 Jersey 上传它们
【发布时间】:2014-11-03 03:51:55
【问题描述】:

我需要有关使用 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

【问题讨论】:

    标签: java rest jersey multipartform-data multiple-file-upload


    【解决方案1】:

    这件事对我很有效:

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

    【讨论】:

      【解决方案2】:

      我可以使用“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]);
      

      希望对你有帮助

      【讨论】:

      • 获取文件内容使用:InputStream is = bodyPart.getEntityAs(InputStream.class);
      • 当我尝试将 inputStream 作为参数传递时,我得到一个运行时异常,它说“DataHead$ReadMultiStream”不可序列化。有什么想法吗?
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        相关资源
        最近更新 更多