日期:2014-05-16  浏览次数:20483 次

struts2 从数据库中读取图片流文件,并显示到jsp
弹出保存路径:

js:

window.open(str,'blank_','scrollbars=no,resizable=no,width=10,height=10,menubar=no');

-------------------------

struts中:

//图片

response.setContentType("image/jpg"); 
      response.setHeader("Content-disposition",  
                        "attachment;filename=\"" + mail.getFileName() + "\";");  
      BufferedImage image = null;
         image=ImageIO.read(in);
         ServletOutputStream sos = response.getOutputStream();
         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
         encoder.encode(image);
         in.close();



//excel

response.setContentType("application/vnd.ms-excel"); 
      response.setHeader("Content-disposition",  
                        "attachment;filename=\"" + mail.getFileName() + "\";");  
      OutputStream out = response.getOutputStream();  
      HSSFWorkbook wb =  new HSSFWorkbook();
         HSSFSheet s = wb.createSheet();
         int len = 0;
         byte[] buf = new byte[1];
      while((len = in.read(buf)) != -1)
      {
       out.write(buf, 0, len);
      }
         wb.write(out);
         out.close();
         return null;



---------------------------------------------------

将图片流从数控库取出,显示到JSP

//jpg

HttpServletResponse response = (HttpServletResponse)
    ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);

  byte[] buf = new byte[1];
            response.setContentType("image/jpeg");
            OutputStream os=response.getOutputStream();
            int len = 0;
          while((len = in.read(buf)) != -1)
          {
             os.write(buf, 0, len);
            }
            os.close();
            in.close();
            return null;



---jsp

<s:property value="response.getOutputStream()"/>



//text file



BufferedReader reader = new BufferedReader(new InputStreamReader(in));
          StringBuilder sb = new StringBuilder();   
          String line = null; &nb