【问题标题】:Zip file cannot be unzipped after uploading via Dropwizard framework通过 Dropwizard 框架上传后无法解压缩 Zip 文件
【发布时间】:2018-03-31 07:11:42
【问题描述】:

我遇到了在服务器上上传 zip 文件后无法解压缩的问题。 我有基于 Dropwizard 框架的 REST API 和下一个端点示例:

@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/zip")
public class ImportResource {

    @POST
    public Response fileService(@FormDataParam("fileData") InputStream fileDataInputStream,
        @FormDataParam("fileData") FormDataContentDisposition fileDataDetail) {

        File newFile = new File("/Users/alexx/Documents/"+ fileDataDetail.getFileName());

        try {
            final OutputStream out = new FileOutputStream(newFile);
            ByteStreams.copy(fileDataInputStream, out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return Response.ok().build();
    }
    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) throws IOException {
        int read;
        final int BUFFER_LENGTH = 1024;
        final byte[] buffer = new byte[BUFFER_LENGTH];
        OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        out.flush();
        out.close();
    }

我还在Application类中提供了适当的类:

....
bootstrap.addBundle(new MultiPartBundle());
....
environment.jersey().register(MultiPartFeature.class);

上传zip文件后,好像上传了,但是解压不了,收到下一条消息:

(错误 1 ​​- 不允许操作)。

文本和图像文件上传并正确打开。

我有没有跳过什么?我应该添加额外的参数还是在某处设置? 感谢您的回答!

【问题讨论】:

    标签: java file-upload jersey zip dropwizard


    【解决方案1】:

    这是我的错。

    我发现过滤器可以在处理资源之前与有效负载一起使用。

    过滤器有下一个代码:

        payload = IOUtils.toString(requestContext.getEntityStream(), StandardCharsets.UTF_8);
                if (payload != null) {
                    RequestPayloadHolder.getRequestPayload().setPayload(payload);
                    InputStream in = IOUtils.toInputStream(payload, StandardCharsets.UTF_8);
                    requestContext.setEntityStream(in);
                }
    

    因此,具有 multipart/form-data 的实体无法正确传输到 String 等。

    在此请求实现之前,所有其他请求负载都是 JSON。

    我必须更加小心。

    【讨论】:

      猜你喜欢
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      相关资源
      最近更新 更多