Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: jQuery file Servlet
imagedownloadservlet.java
 
package servlets;
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.....
<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........
<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 5-Jul-13 2:49am
Edited 5-Jul-13 9:58am
(no name)117.7K
v2

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 5 Jul 2013
Copyright © CodeProject, 1999-2014
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