1

How can i download image to local computer when click on a image in java?

Image store in a folder name images in web-inf, and this image given in a link as src. When i click on this link i can download this images to local computer. any on can help me??

<a href="<%=request.getContextPath()%>/pages/imageDownload.jsp"> Image download </a>
this is the link to next page.

<img src="images/abt.jpg" width="300" height="100" alt="" />

This is the image link.. I want to download image when click on this link.

Thanks in advance..

anoop
  • 782
  • 4
  • 12
  • 35

2 Answers2

0

See this answer: https://stackoverflow.com/a/1134128/34088

In your case, you should set the content-type to image/jpeg

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

When you wrap an image with a href, on clicking that link you can view that image in the web browser itself. To download it you will have to right click and give save link as and then choose the directory where you want to store the file.
But if you strictly want to download on the click of the link itself, the you can use a form.
For example :
give the link location to a servlet called containing :


In "your servlet"'s doGet() method, do as Aaron Digulla has pointed out https://stackoverflow.com/a/1134128/34088 use :

byte[] data = getBinaryFromSomeWhere(request.getParameter("xyz"));
response.setContentType("image/jpeg");
request.setHeader("Content-length", Integer.toString(data.length));
out.write( data, start, end );
out.flush();

The file will be saved in your system.

Community
  • 1
  • 1
Ashwin
  • 12,691
  • 31
  • 118
  • 190