Chinaunix首页 | 论坛 | 博客
  • 博客访问: 456632
  • 博文数量: 750
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4970
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:36
文章分类

全部博文(750)

文章存档

2011年(1)

2008年(749)

我的朋友
最近访客

分类:

2008-09-09 15:24:30


  这个例子是通过用Struts的FormFile来写入到MySQL中。。。
  
  用用户通过选一个图片,然后按submit就可以存入数据库中
  
  其中先要建立一个表:
  
  create table test ( name varchar(20), pic blob );在MySQL的test库中
  
  <%@ page language="java"%>
  <%@ taglib uri="" prefix="bean"%>
  <%@ taglib uri="" prefix="html"%>
  
  
  
  JSP for multiForm form
  
  
  一定要用enctype=“multipart/form-data“不然就提交之后就会有抛出异常
  file :

  name :

  
  

  
  
  
  2. 相对应的ActionForm:
  
  //Created by MyEclipse Struts
  // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.1/xslt/Class.xsl
  
  package saoo.struts.form;
  
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.upload.FormFile;
  
  /**
  * MyEclipse Struts
  * Creation date: 08-24-2004
  *
  * XDoclet definition:
  * @struts:form name="multiForm"
  */
  public class MultiForm extends ActionForm {
  
  // --------------------------------------------------------- Instance Variables
  
  /** file property */
  private FormFile file;
  
  /** name property */
  private String name;
  
  // --------------------------------------------------------- Methods
  
  /**
  * Returns the file.
  * @return FormFile
  */
  public FormFile getFile() {
  return file;
  }
  
  /**
  * Set the file.
  * @param file The file to set
  */
  public void setFile(FormFile file) {
  this.file = file;
  }
  
  /**
  * Returns the name.
  * @return String
  */
  public String getName() {
  return name;
  }
  
  /**
  * Set the name.
  * @param name The name to set
  */
  public void setName(String name) {
  this.name = name;
  }
  }
  
  3. 对就的Action:
  
  //Created by MyEclipse Struts
  // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.1/xslt/Class.xsl
  
  package saoo.struts.action;
  
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  import org.apache.struts.upload.FormFile;
  
  import saoo.struts.form.MultiForm;
  
  /**
  * MyEclipse Struts
  * Creation date: 08-24-2004
  *
  * XDoclet definition:
  * @struts:action path="/multi" name="multiForm" input="/form/multi.jsp" scope="request"
  */
  public class MultiAction extends Action {
  
  // --------------------------------------------------------- Instance Variables
  
  // --------------------------------------------------------- Methods
  
  /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
  public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) {
  MultiForm multiForm = (MultiForm) form;
  FormFile file = multiForm.getFile();
  
  String name = multiForm.getName();
  try {
  Class.forName("org.gjt.mm.mysql.Driver");
  String url="jdbc:mysql:///test";
  Connection con=DriverManager.getConnection(url,"root","password");
  String sql="insert into pic values (?,?)";
  PreparedStatement ps =con.prepareStatement(sql);
  ps.setString(1, name);
  //加入图片到数据库
  ps.setBinaryStream(2,file.getInputStream(),file.getFileSize());
  ps.executeUpdate();
  ps.close();
  con.close();
  } catch (SQLException se) {
  se.printStackTrace();
  return mapping.findForward("error");
  } catch (ClassNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return mapping.findForward("error");
  } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return mapping.findForward("error");
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  return mapping.findForward("error");
  }
  return mapping.findForward("success");
  }
  }
【责编:admin】

--------------------next---------------------

阅读(226) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~