struts.xml代码:

<action name="getDocumentImgByFileId" class="checkInfoAction" method="getDocumentImgByFileId">
<result type="stream" name="SUCCESS">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>

action类代码:

  1. /** 
  2.  * 获取图片流 
  3.  * @return 
  4.  */  
  5. public String getDocumentImg() {  
  6.     model = (JcOffDocument)baseBO.load(model);  
  7.     String imgUrl = getAllPath(model);  
  8.     //String imgUrl = "/jczfoa/images/aa.jpg";  
  9.       
  10.     if(!MyUtils.isFileExist(imgUrl, "")){//如果指定的图片不存在,显示默认图片  
  11.         imgUrl = getSession().getServletContext().getRealPath("/")+"images"+File.separator+"zw.jpg";  
  12.     }  
  13.       
  14.     try {  
  15.         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  16.         InputStream input = new BufferedInputStream(new FileInputStream(imgUrl));  
  17.         byte[] bt = new byte[1024];  
  18.         while (input.read(bt) > 0) {  
  19.             bos.write(bt);  
  20.         }  
  21.         this.inputStream = new ByteArrayInputStream(bos.toByteArray());  
  22.         bos.close();  
  23.         input.close();  
  24.     } catch (Exception e) {  
  25.         // TODO Auto-generated catch block  
  26.         e.printStackTrace();  
  27.     }  
  28.     return SUCCESS;  
  29. }  
  30. private ByteArrayInputStream inputStream;  
  31.       
  32. public ByteArrayInputStream getInputStream() {  
  33.     return inputStream;  
  34. }  
  35. public void setInputStream(ByteArrayInputStream inputStream) {  
  36.     this.inputStream = inputStream;  
  37. }

 

posted on 2017-07-13 10:17  素雅柠檬  阅读(1778)  评论(0编辑  收藏  举报