In JAX-RS, for user to download an image file, annotate the method with @Produces("image/image-type")
:
- Put @Produces(“image/png”) on service method, for “png” image.
- Set “Content-Disposition” in Response header to prompt a download box.
Note
For other image types, refer to this list of the image types
1. Download Image in JAX-RS
Full example to download an image file from JAX-RS.
import java.io.File; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("/image") public class ImageService { private static final String FILE_PATH = "c:\\favtuts-logo.png"; @GET @Path("/get") @Produces("image/png") public Response getFile() { File file = new File(FILE_PATH); ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition", "attachment; filename=image_from_server.png"); return response.build(); } }
2. Demo
Access this URI pattern : “/image/get“.
Figure : Image file “c:\\favtuts-logo.png” from server is prompted for user to download, with a new file name “image_from_server.png“

Download Source Code
$ git clone https://github.com/favtuts/java-jax-rs-tutorials.git
$ cd jax-rs-download-imagefile-example