编程语言
首页 > 编程语言> > java-Dropwizard 0.8.1:多个文件上传

java-Dropwizard 0.8.1:多个文件上传

作者:互联网

我正在使用dropwizard,我想一次上传多个文件.

如何更改我的代码以上传多个文件?

我正在使用org.glassfish.jersey.media”,“ jersey-media-multipart”,“ 2.17”
 用于文件上传.

这是我上传单个文件的代码:

@Path("/uploadPhoto")
@ApiOperation(
        value = "Upload a photo for an Ad",
        response = Response.class)
@POST
@Timed
@UnitOfWork
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response fileUploaded(@FormDataParam("file") final InputStream inputStream,
                         @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
    List<AdImage> images = new ArrayList<AdImage>();

        images.add(writeImageAndSave(inputStream
                , contentDispositionHeader));
    return Response.ok(toJson(images), MediaType.APPLICATION_JSON).build();

}

解决方法:

我发现这里是代码:

    @Path("/uploadPhoto")
@ApiOperation(
        value = "Upload a photo for an Ad",
        response = Response.class)
@POST
@Timed
@UnitOfWork
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(FormDataMultiPart multiPart) {


    List<AdImage> images = new ArrayList<AdImage>();
    List<FormDataBodyPart> bodyParts =
            multiPart.getFields("file");
    for (FormDataBodyPart part : bodyParts) {
        images.add(writeImageAndSave(part.getValueAs(InputStream.class
        ), part.getFormDataContentDisposition()));
    }

    return Response.ok(toJson(images), MediaType.APPLICATION_JSON).build();
}

标签:jersey,jersey-2-0,dropwizard,java
来源: https://codeday.me/bug/20191120/2042950.html