0
我需要讀取一個zip文件並將其作爲Jersey響應發送。下面的代碼片段返回一個zip文件。但是壓縮文件(sample1.zip)無法打開,因爲它報告「壓縮文件未知格式或損壞。」我可以在我的機器上查看其他zip文件。任何人都可以幫我弄清楚問題可能是什麼?Jersey ws返回一個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公地IO實用程序(IOUtils)輸入複製到輸出中。