Package donghongyujava. servlet; import java. io. file; public class ShowImage extends HttpServlet {/*** Constructor of the object. */public ShowImage () {super ();}/*** Destruction of the servlet. <br> */public void destroy () {super. destroy (); // Just puts "destroy" string in log // Put your code here} public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOE Xception {// showImage (request, response); downlodeImage (request, response);} public void showImage (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// read method // response. setContentType ("application/zip"); response. setContentType ("image/jpeg"); // obtain the absolute path of the image. String path = this. getServletContext (). getRealPath ("/"); File file = new File (path + "/images/WIN_20131027 _ 214421. JPG "); // create a file input stream FileInputStream is = new FileInputStream (file); // response output stream ServletOutputStream out = response. getOutputStream (); // create a buffer byte [] buffer = new byte [1024]; int len = 0; while (len = is. read (buffer ))! =-1) {out. write (buffer, 0, len);} is. close (); out. flush (); out. close ();} public void downlodeImage (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// get the absolute path of the image String path = this. getServletContext (). getRealPath ("/"); File file = new File (path + "/images/WIN_20131027_214421.JPG"); // sets the header information and content processing method, the attachment is opened as an attachment, which is to download and set the name of the downloaded file to response. setHeader ("Content- Disposition "," attachment; filename = "+ file. getName (); // create a file input stream FileInputStream is = new FileInputStream (file); // response output stream ServletOutputStream out = response. getOutputStream (); // create a buffer byte [] buffer = new byte [1024]; int len = 0; while (len = is. read (buffer ))! =-1) {out. write (buffer, 0, len);} is. close (); out. flush (); out. close ();} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this. doGet (request, response );}}