Main Tutorials

Download image file from JAX-RS

In JAX-RS, for user to download an image file, annotate the method with @Produces("image/image-type") :

  1. Put @Produces(“image/png”) on service method, for “png” image.
  2. 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:\\mkyong-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:\\mkyong-logo.png” from server is prompted for user to download, with a new file name “image_from_server.png

download image from server

Download Source Code

Download it – JAX-RS-Download-ImageFile-Example.zip (6 KB)

References

  1. JAX-RS @Produces JavaDoc
  2. list of the images types

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Follower
7 years ago

Hi MKyong, thank you for the post. can we open the file in default image viewer, without the prompt when the URL is clicked. Spring rest api does that, but didnot find any document for JaxRS. Please advice.

Mac
8 years ago

I need to send a file which is downloadable at client side. My code works fine on localhost as shown here, but not working on web application. Please advise.

mauro
9 years ago

bit if i would get a image as stream for show at a web page and not save as file , how i can resolve? Suppose that the image is saved into a DB with JPA entitty . Into the record of table db is saved also the mymetype of image( jpg. bmp. gif, png ). SO i not can use

@Produces(“image/png”)

because the image can be many types .( jpg. bmp. gif, png ).

I not would prompt for user to download, with a new file name “image_from_server.png“, but show into a page web .

I would show the image with tag

tank you for your attention.

Sourav Ken
9 years ago

Good post. Really helped. Just wondering how good it is to call images via restful service. As for every image it need to do processing, so will it not be extra burden on server.

gporter
10 years ago

thank you to post mkyong.Its really perfect but i cant use on global file path.Could you show that ?

sebastian
12 years ago

Can i use @POST instead of @GET ? For me its not working, when i used @POST

Ram
12 years ago

Thanks for the example. It works perfectly.

I tried to enhance it further and added role based security to the service. Now I am unable to download the file. I get a pop-up which says “Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.”

Any pointers regarding what I might be missing?

Thanks.

Hambam
12 years ago

Does this code work?

I dont think so, because file is just “an abstract representation of file and directory pathnames” – no data is read….

Right now I need to upload a file from the server to the client using resteasy.
How can I read data into a File or which kind of java object do I need to return to the client??

Best regards

srini
10 years ago
Reply to  mkyong

In my case its working fine.
but when try to download a file, new tab is opening after that the popup is coming to download. how can I stop it going to new tab ?