servlet 简单案例!实现图片下载功能

来源:互联网 发布:淘宝千牛官网 编辑:程序博客网 时间:2024/05/18 02:53
  1.  
  2. /*
  3. *Servlet实现将图片写入到网页和实现图片下载的功能
  4. *
  5. */
  6.  
  7. package lipf.java.servlet;  
  8.   
  9. import java.io.File;  
  10.   
  11. public class ShowImage extends HttpServlet {  
  12.   
  13.     /** 
  14.      * Constructor of the object. 
  15.      */  
  16.     public ShowImage() {  
  17.         super();  
  18.     }  
  19.   
  20.     /** 
  21.      * Destruction of the servlet. <br> 
  22.      */  
  23.     public void destroy() {  
  24.         super.destroy(); // Just puts "destroy" string in log  
  25.         // Put your code here  
  26.     }  
  27.   
  28.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  29.             throws ServletException, IOException {  
  30.   
  31. //      showImage(request, response);  
  32.         downlodeImage(request, response);  
  33.     }  
  34.   
  35.     public void showImage(HttpServletRequest request,  
  36.             HttpServletResponse response) throws ServletException, IOException {  
  37.         // 读取方式  
  38.         // response.setContentType("application/zip");  
  39.         response.setContentType("image/jpeg");  
  40.         // 获取图片绝对路径  
  41.         String path = this.getServletContext().getRealPath("/");  
  42.         File file = new File(path + "/images/123.JPG");  
  43.         // 创建文件输入流  
  44.         FileInputStream is = new FileInputStream(file);  
  45.         // 响应输出流  
  46.         ServletOutputStream out = response.getOutputStream();  
  47.         // 创建缓冲区  
  48.         byte[] buffer = new byte[1024];  
  49.         int len = 0;  
  50.         while ((len = is.read(buffer)) != -1) {  
  51.             out.write(buffer, 0, len);  
  52.         }  
  53.         is.close();  
  54.         out.flush();  
  55.         out.close();  
  56.   
  57.     }  
  58.   
  59.     public void downlodeImage(HttpServletRequest request,  
  60.             HttpServletResponse response) throws ServletException, IOException {  
  61.           
  62.         // 获取图片绝对路径  
  63.         String path = this.getServletContext().getRealPath("/");  
  64.         File file = new File(path + "/images/123.JPG");  
  65.         //设置头信息,内容处理的方式,attachment以附件的形式打开,就是进行下载,并设置下载文件的命名  
  66.         response.setHeader("Content-Disposition","attachment;filename="+file.getName());  
  67.         // 创建文件输入流  
  68.         FileInputStream is = new FileInputStream(file);  
  69.         // 响应输出流  
  70.         ServletOutputStream out = response.getOutputStream();  
  71.         // 创建缓冲区  
  72.         byte[] buffer = new byte[1024];  
  73.         int len = 0;  
  74.         while ((len = is.read(buffer)) != -1) {  
  75.             out.write(buffer, 0, len);  
  76.         }  
  77.         is.close();  
  78.         out.flush();  
  79.         out.close();  
  80.   
  81.     }  
  82.   
  83.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  84.             throws ServletException, IOException {  
  85.   
  86.         this.doGet(request, response);  
  87.     }  
  88.   
  89. }  
原创粉丝点击