【问题标题】:Dropwizard 0.8.1 : multiple file uploadDropwizard 0.8.1:多文件上传
【发布时间】:2015-05-18 16:22:01
【问题描述】:

我正在使用 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();

}

【问题讨论】:

    标签: java jersey jersey-2.0 dropwizard


    【解决方案1】:

    我在这里找到了代码:

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

    【讨论】:

    • 您或其他人知道如何在没有 FormDataMultiPart 的情况下做同样的事情吗? FormDataMultiPart 包括有一个 HTML 表单,但在某些机器对机器的情况下,没有表单。也许像这样-->uploadFile(InputStream[] inStream)
    • @heapach 这对你的情况可能有用stackoverflow.com/questions/18075646/…
    • 谢谢,但这只是一个文件。我需要的是多个文件,但没有 html 表单:-)
    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 2021-01-23
    • 1970-01-01
    • 2016-01-04
    • 2018-03-31
    • 2015-02-01
    • 2012-09-09
    相关资源
    最近更新 更多