How can I return a Zip file from my Java server-side using JAX-RS?
Asked Answered
R

4

8

I want to return a zipped file from my server-side java using JAX-RS to the client.

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?

Rebane answered 5/4, 2014 at 13:24 Comment(0)
R
11

You are delegating in Jersey the knowledge of how to serialize the ZipOutputStream. So, with your code you need to implement a custom MessageBodyWriter for ZipOutputStream. 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.

Raneeraney answered 6/4, 2014 at 19:30 Comment(1)
This was the only working solution for me. I tried all posted solutions with ZipOutputStream, just Apache FileUtils worked. Thanks!Modifier
A
10

You can write the attachment data to StreamingOutput class, which Jersey will read from.

@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();
}
Angioma answered 5/1, 2018 at 20:41 Comment(1)
Thanks! The only bit I had to change is that zipOut.write takes bytes.Monster
G
3

In Jersey 2.16 file download is very easy

Below is the example for the ZIP file

@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();
}
Gangrel answered 12/2, 2015 at 14:21 Comment(0)
D
1

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.

Daphene answered 5/4, 2014 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.