Servlet allows you to write images to webpages and download images.

Source: Internet
Author: User

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 );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.