[英]How can I return a Zip file from my Java server-side using JAX-RS?
I want to return a zipped file from my server-side java using JAX-RS to the client. 我想使用JAX-RS从服务器端Java返回压缩文件到客户端。
I tried the following code, 我尝试了以下代码,
@GET
public Response get() throws Exception {
final String filePath = "C:/MyFolder/My_File.zip";
final File file = new File(filePath);
final ZipOutputStream zop = new ZipOutputStream(new FileOutputStream(file);
ResponseBuilder response = Response.ok(zop);
response.header("Content-Type", "application/zip");
response.header("Content-Disposition", "inline; filename=" + file.getName());
return response.build();
}
But i'm getting exception as below, 但是我遇到了如下异常,
SEVERE: A message body writer for Java class java.util.zip.ZipOutputStream, and Java type class java.util.zip.ZipOutputStream, and MIME media type application/zip was not found
SEVERE: The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
What is wrong and how can I fix this? 有什么问题,我该如何解决?
You are delegating in Jersey the knowledge of how to serialize the ZipOutputStream. 您正在泽西(Jersey)委派有关如何序列化ZipOutputStream的知识。 So, with your code you need to implement a custom MessageBodyWriter for ZipOutputStream.
因此,对于您的代码,您需要为ZipOutputStream实现自定义MessageBodyWriter。 Instead, the most reasonable option might be to return the byte array as the entity.
相反,最合理的选择可能是将字节数组作为实体返回。
Your code looks like: 您的代码如下所示:
@GET
public Response get() throws Exception {
final File file = new File(filePath);
return Response
.ok(FileUtils.readFileToByteArray(file))
.type("application/zip")
.header("Content-Disposition", "attachment; filename=\"filename.zip\"")
.build();
}
In this example I use FileUtils from Apache Commons IO to convert File to byte[], but you can use another implementation. 在此示例中,我使用来自Apache Commons IO的 FileUtils将File转换为byte [],但是您可以使用其他实现。
In Jersey 2.16 file download is very easy 在Jersey 2.16中文件下载非常容易
Below is the example for the ZIP file 以下是ZIP文件的示例
@GET
@Path("zipFile")
@Produces("application/zip")
public Response getFile() {
File f = new File(ZIP_FILE_PATH);
if (!f.exists()) {
throw new WebApplicationException(404);
}
return Response.ok(f)
.header("Content-Disposition",
"attachment; filename=server.zip").build();
}
You can write the attachment data to StreamingOutput class, which Jersey will read from. 您可以将附件数据写入StreamingOutput类,Jersey将从中读取。
@Path("/report")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response generateReport() {
String data = "file contents"; // data can be obtained from an input stream too.
StreamingOutput streamingOutput = outputStream -> {
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(outputStream));
ZipEntry zipEntry = new ZipEntry(reportData.getFileName());
zipOut.putNextEntry(zipEntry);
zipOut.write(data); // you can set the data from another input stream
zipOut.closeEntry();
zipOut.close();
outputStream.flush();
outputStream.close();
};
return Response.ok(streamingOutput)
.type(MediaType.TEXT_PLAIN)
.header("Content-Disposition","attachment; filename=\"file.zip\"")
.build();
}
I'm not sure I it's possible in Jersey to just return a stream as result of annotated method. 我不确定是否有可能在泽西岛返回带注释方法的流。 I suppose that rather stream should be opened and content of the file written to the stream.
我想应该打开流,并将文件内容写入该流。 Have a look at this blog post.
看看这篇博客文章。 I guess You should implement something similar.
我想您应该实现类似的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.