Click here to Skip to main content
12,999,845 members (49,717 online)
Rate this:
Please Sign up or sign in to vote.
See more:

package servlets;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageDownloadServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException 
  // obtains ServletContext
  ServletContext context = getServletContext();
  // reads input file from an absolute path
    String filePath = request.getParameter("filepath");
    File downloadFile = new File(filePath);
    FileInputStream inStream = new FileInputStream(downloadFile);
  // gets MIME type of the file
    String mimeType = context.getMimeType(filePath);
    if (mimeType == null) {   
   // set to binary type if MIME mapping not found
     mimeType = "application/octet-stream";
    System.out.println("MIME type: " + mimeType);
  // modifies response
    response.setContentLength((int) downloadFile.length());
  // forces download
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
    response.setHeader(headerKey, headerValue);
  // obtains response's output stream
    OutputStream outStream = response.getOutputStream();
  byte[] buffer = new byte[4096];
  int bytesRead = -1;
  while ((bytesRead = != -1) {
   outStream.write(buffer, 0, bytesRead);

This is my jquery code.......

$("#save").on("click",function (){
alert("Data: " + data + "\nStatus: " + status);

This is my html code.....
<div id="rightoutputimgae">
<div id="rightimgId" class="rightimg"  rel="tooltip" content="<img src='jqe13/image/1.jpg' class='tooltip-image'/> ">
<div id="outputimageId" class="outputimage"><img src="jqe13/image/1.jpg" alt="Right Bottom Image"></div>
<ul><li id="outcheckbox"><input name="outCheck" type="checkbox"></li>
<li id="outedit"><a href="#"><img src="jqe13/image/edit_s.PNG" alt="edit" title="Edit"></a></li>
<li id="outdelete"><a href="#"><img src="jqe13/image/delet_c.PNG" alt="delete" title="Delete"></a></li>
<li id="outfullscreen"><a href="#"><img src="jqe13/image/fullscreen_c.PNG" alt="Full Screen" class="fullscreen" title="Full Screen"></a></li>
<li id="outshare"><a href="#"><img src="jqe13/image/share_c.PNG" alt="Share" title="Share"></a>
<div id="menu">
<div id="tooltip_menu">
        <a href="#" class="menu_top" id="email"><img src="jqe13/image/email.PNG" alt="Email" title="Email"></a>
        <a href="#" onClick="postToFeed()" class="facebook"><img src="jqe13/image/fb.PNG" alt="Facebook" title="Facebook"></a>
        <a href="#" id="twitter"><img src="jqe13/image/twitter.png" alt="Twitter" title="Twitter"></a>
        <a href="#" class="menu_bottom" id="save"><img src="jqe13/image/save.PNG" alt="Save" title="Save"></a>
<li id="outprint"><a href="#" class="printMe"><img src="jqe13/image/print.PNG" alt="Print" title="Print"></a></li>

This is my Servlet mapping........

i want to download a image to client system....when click on save button.....
i tried above code but not getting anything.............can any one help..............
Posted 5-Jul-13 1:49am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 5 Jul 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100