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

后端 未结 4 550
自闭症患者
自闭症患者 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:39

    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();
    }
    

提交回复
热议问题