ssh上传并显示图片

使用的是将图片保存到服务器,并记录路径名的方式,代码如下:

struts部分:

 <form-bean name="upfileForm" type="org.apache.struts.validator.DynaValidatorForm">
      <form-property name="filename" type="java.lang.String" />
      <form-property name="filedata" type="org.apache.struts.upload.FormFile" />
    </form-bean>

    <action
      attribute="upfileForm"
      input="/upload/uploadfile.jsp"
      name="upfileForm"
      path="/upfile"
      scope="request"
      validate="true"
      type="com.yourcompany.struts.action.UpfileAction">
      <forward name="ok" path="/upload/ok.jsp" />
    </action>

    <action path="/displayimg" type="com.yourcompany.struts.action.DisplayimgAction">
      <forward name="ok" path="/upload/displayimg.jsp" />
    </action>

 

public class UpfileAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  DynaActionForm upfileForm = (DynaActionForm) form;// TODO Auto-generated method stub  
   Image image = new Image();

   String strimgname = upfileForm.getString("filename");
   image.setImgname(strimgname);
   imageDAO.save(image); 
  FormFile filedata = (FormFile)upfileForm.get("filedata");
   ServletContext servletContext = this.getServlet().getServletContext();
   String filePath = servletContext.getRealPath("/");
   try {
    InputStream stream = filedata.getInputStream();
    OutputStream bos = new FileOutputStream(filePath + "/image/" + filedata.getFileName());
     int bytesRead = 0;
             byte[] buffer = new byte[8192];
             while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
             bos.write(buffer, 0, bytesRead);
             }
             bos.close();
             stream.close();
   }catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return mapping.findForward("ok");
 }
 
 private ImageDAO imageDAO;
 public void setImageDAO(ImageDAO dao){
  this.imageDAO = dao;  
 }
 
}


 

public class DisplayimgAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
  List results = imageDAO.findAll();
  if(results!=null){
   HttpSession session=request.getSession();
            session.setAttribute("results",results);
            return mapping.findForward("ok");
  }return mapping.findForward("fail");
 }
 
 private ImageDAO imageDAO;
 public void setImageDAO(ImageDAO dao){
  this.imageDAO = dao;  
 }
 
}


ImageDAO.java

 public void save(Image transientInstance) {
     Session session =this.getSession();
     Transaction tx = null;
     tx = session.beginTransaction();
     session.save(transientInstance);
     tx.commit();
     session.evict(transientInstance);
 }

 public List findAll() {
  Session session =this.getSession();
  Query query = session.createQuery("from Image");
  return query.list();
 }

displayimg.jsp

<logic:iterate id="element" name="results">
    <tr>
   <td width="100"><bean:write name="element" property="id"/> </td>
   <td width="100"><img  src="image/<bean:write  name='element' property='imgname'/>"/></td>
   </tr>
</logic:iterate>

uploadfile.jsp

<html:form action="/upfile" enctype = "multipart/form-data">
   filename : <html:text  property="filename"/><html:errors property="filename"/><br/>
   filedata : <html:file property="filedata"/><html:errors property="filedata"/><br/>
   <html:submit/><html:cancel/>
  </html:form>

<!-- u-f8 filter -->
  <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>

 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>*.jsp</url-pattern>
 </filter-mapping>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值