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

后端 未结 4 554
自闭症患者
自闭症患者 2021-01-19 04:12

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 Exc         


        
4条回答
  •  悲&欢浪女
    2021-01-19 04:51

    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.

提交回复
热议问题