简体   繁体   English

如何使用JAX-RS从Java服务器端返回Zip文件?

[英]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.

相关问题 如何使用JAX-RS(Jersey)编写服务器端Java代码以将任何类型的文件作为流返回? - How can I write a server side java code to return any kind of file as stream using JAX-RS(Jersey)? 使用Jax-rs从ZipOutPutStream下载Zip文件 - Downloading Zip file From ZipOutPutStream using Jax-rs JAX-RS端点触发服务器端操作 - JAX-RS Endpoint triggering server-side actions 如何使用http POST JAX-RS将对象从AngularJS服务传递给Java - How can I pass an Object from an AngularJS Service to Java using http POST JAX-RS 我可以从JAX-RS调用返回Date对象吗? - Can I return a Date object from a JAX-RS call? 如何使用JAX-RS返回实际的html文件 - How to return actual html file using JAX-RS 如何使用Jersey JAX-RS返回结果集? - How do I return a resultset using Jersey JAX-RS? JAX-RS:如何将对象列表作为JSON返回? - JAX-RS: How can I return my list of objects as JSON? 如何让JAX-RS将Java 8 LocalDateTime属性作为JavaScript样式的日期字符串返回? - How can I have JAX-RS return a Java 8 LocalDateTime property as a JavaScript-style Date String? 如何通过JAX-RS(Jersey)使用$ resource从AngularJS客户端使用JSON数据和文件上传向服务器发送请求? - How can I send request to the server with JSON data and fileupload from AngularJS client using $resource via JAX-RS(Jersey)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM