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

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>

posted on 2008-01-17 17:44 lzj520 阅读(2612) 评论(1)  编辑  收藏 所属分类: Struts1Spring个人学习日记Hibernate

只有注册用户登录后才能发表评论。


网站导航: