Jersey ws 返回 zip 文件
我需要读取 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 实用程序(IOUtils)将输入复制到 ouput 。
I need to read a zip file and send it as a Jersey response.The below code snippet returns a zip file. But the zip file(sample1.zip) cannot be opened as it reports "The archive is either unknown format or damaged." I'm able to other zip files on my machine. Can anyone help me to figure out what the issue could be?
@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();
}
I've used apache commons io utility (IOUtils) to copy input to ouput .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否定义了 @Produces("application/zip") 的 @Provider ?
检查日志,看看是否需要messagebodywriter
have you defined the @Provider that's @Produces("application/zip")?
Check the log, and look if a messagebodywriter is needed