我需要读取一个压缩文件,并将其作为一个泽西response.The下面的代码片段返回一个压缩文件。但是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实用程序(IOUtils)将输入复制到输出。
发布于 2012-02-17 22:32:20
您是否定义了@Produces的@Provider (“application/zip”)?检查日志,查看是否需要messagebodywriter
https://stackoverflow.com/questions/9266171
复制相似问题