我需要读取一个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();
}
我已经使用apache commons io utility(IOUtils)将输入复制到输出。
答案 0 :(得分:0)
你定义了@Produces(“application / zip”)的@Provider吗? 检查日志,看看是否需要messagebodywriter