java 多文件上传使用@Context HttpServletRequest 和@FormDataParam 在球衣

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17174485/
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-01 01:12:37  来源:igfitidea点击:

Multiple File Upload using @Context HttpServletRequest with @FormDataParam in jersey

javaspring-mvcjerseyjax-rs

提问by agpt

I have created a jersey restful web service where I managedto upload multiple number of filesusing @Context HttpServletRequest requestas method signature which work nicely.
Thing is, to fetch other form fields I need to repetitively check with .isFormField();method with relative .getName();for file or .getFieldName();, and .getString();method to check whether required fields are present or not every time the web service is calledwhich I think little lengthy and expensive process if there are several other fields.

我创建了一个 jersey restful web 服务,我设法使用方法签名上传了多个文件,这些文件@Context HttpServletRequest request工作得很好。
问题是,要获取其他表单字段,我需要.isFormField();使用相对.getName();文件或方法重复检查.getFieldName();,以及每次调用 Web 服务时.getString();检查所需字段是否存在的方法,我认为如果有其他几个领域。

Easier approach was to use @FormDataParamwhere webservice used to exposed with parameter which client need to pass but problemis I am not able to upload more than one file at one go.

更简单的方法是使用@FormDataParamwebservice 过去使用客户端需要传递的参数公开的地方,但问题是我无法一次上传多个文件。

Since Its also not possible to use request.getParameter("field1");to get other form fields if media type or enctype is multipart/form-data.

因为request.getParameter("field1");如果 media type 或 enctype 是multipart/form-data.

Whenever I tried to combine both @FormDataParamand @Context HttpServletRequest requesttogether, it throws exception:
org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
while parsing the requestwith .parseRequest(request);method of ServletFileUploadclass.

每当我试图结合两者@FormDataParam@Context HttpServletRequest request在一起,它抛出异常:
org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
在解析请求.parseRequest(request);的方法ServletFileUpload类。

Kindly suggest some good approach How can I achieve multiple file upload with getting required form fields as easy as @FormDataParamin jersey.

请提出一些好的方法如何通过像@FormDataParam球衣一样简单地获取所需的表单字段来实现多个文件上传。

approach for multiple file upload:

多文件上传方法:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/multipleFiles")
public String restDemo(@Context HttpServletRequest request) 
{
  //...code goes here
}

My form:

我的表格:

enter image description here

在此处输入图片说明

output:(after parsing request)

输出:(解析请求后)

field1 > abc
field2 > xyz
Chrysanthemum.jpg Size: 879394
Desert.jpg Size: 845941
Hydrangeas.jpg Size: 595284
Jellyfish.jpg Size: 775702

field1 > abc
field2 > xyz
Chrysanthemum.jpg 尺寸:879394
Desert.jpg 尺寸:845941
Hydrangeas.jpg 尺寸:595284
Jellyfish.jpg 尺寸:775702

回答by Marcos Zolnowski

If the fields have the same name, like this:

如果字段具有相同的名称,如下所示:

<form name="formtest" action="rest/multipleFiles" method="POST" enctype="multipart/form-data">
    <input type="text" name="atext" value="abc" />
    <input type="text" name="btext" value="123" />
    <input type="file" name="zfile" value="" />
    <input type="file" name="zfile" value="" />
    <input type="submit" value="submit" />
</form>

You can use:

您可以使用:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/multipleFiles")    
public String restDemo(@FormDataParam("zfile") List<FormDataBodyPart> zfile)

Now, I advise against using HttpServletRequest. If you need to process everything, use this:

现在,我建议不要使用 HttpServletRequest。如果您需要处理所有内容,请使用以下命令:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)    
@Produces(MediaType.APPLICATION_JSON)
@Path("/multipleFiles")
public String restDemo(FormDataMultiPart formParams) {
    formParams.getFields();
}