Web services 返回一个zip文件

Web services 返回一个zip文件,web-services,jersey,Web Services,Jersey,我需要读取一个zip文件并将其作为Jersey响应发送。下面的代码段返回一个zip文件。但是zip文件(sample1.zip)无法打开,因为它报告“归档文件的格式未知或已损坏”。我可以在我的机器上找到其他zip文件。有谁能帮我找出问题所在吗 @Produces("application/zip") public Response getFile() { StreamingOutput soo = new StreamingOutput() { public void w

我需要读取一个zip文件并将其作为Jersey响应发送。下面的代码段返回一个zip文件。但是zip文件(sample1.zip)无法打开,因为它报告“归档文件的格式未知或已损坏”。我可以在我的机器上找到其他zip文件。有谁能帮我找出问题所在吗

@Produces("application/zip")
public Response getFile() {
    StreamingOutput soo = new StreamingOutput() {
        public void write(OutputStream output) throws IOException,
                WebApplicationException {
            try {
                ZipFile zipFile = new ZipFile("sample.zip");
                InputStream in = zipFile.getInputStream(zipFile
                        .getEntry("test.xml"));
                IOUtils.copy(in, output);
            } catch (Exception e) {
                throw new WebApplicationException(e);
            }
        }
    };
    return Response
            .ok()
            .entity(soo)
            .header("Content-Disposition",
                    "attachment; filename = sample1.zip").build();

}

我使用ApacheCommonsIO实用程序(IOUtils)将输入复制到输出

您是否定义了@products的@Provider(“application/zip”)? 检查日志,并查看是否需要messagebodywriter