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 the Author

author image
mkyong
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

avatar
10 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
10 Comment authors
FollowerMacmauroSourav KenJAX-RS TUTORIALS | BUYWORKSELL Recent comment authors
newest oldest most voted
Follower
Guest
Follower

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
Guest
Mac

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
Guest
mauro

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… Read more »

Sourav Ken
Guest
Sourav Ken

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.

trackback
JAX-RS TUTORIALS | BUYWORKSELL

[…] Download image file from JAX-RS Example to output an image file for user to download. […]

gporter
Guest
gporter

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

sebastian
Guest
sebastian

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

Ram
Guest
Ram

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
Guest
Hambam

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

trackback
JAX-RS Tutorial

[…] get HTTP headers.Download text file from JAX-RS Example to output a text file for user to download.Download image file from JAX-RS Example to output an image file for user to download.Download pdf file from JAX-RS Example to […]