Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
imagedownloadservlet.java

package servlets;
Java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
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.setContentType(mimeType);
    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 = inStream.read(buffer)) != -1) {
   outStream.write(buffer, 0, bytesRead);
  }
  
  inStream.close();
  outStream.close();  
 }
}

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

$("#save").on("click",function (){
$.post("/ImageDownloadServlet",
{
filepath:javascript_var
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});

This is my html code.....
HTML
<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>
</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>
</div>
</div>
</li>
<li id="outprint"><a href="#" class="printMe"><img src="jqe13/image/print.PNG" alt="Print" title="Print"></a></li>
</ul>
</div>


This is my Servlet mapping........
HTML
<servlet-mapping>
    <servlet-name>ImageDownloadServlet</servlet-name>
    <url-pattern>/ImageDownloadServlet</url-pattern>
  </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
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900