用Struts向MYSQL数据库中储存图片实例

豆豆网   技术应用频道   2007年08月23日  【字号: 收藏本文

内容摘要:这个例子是通过用Struts的FormFile来写入到MySQL中。用户通过选一个图片,然后按submit就可以存入数据库中

  这个例子是通过用Struts的FormFile来写入到MySQL中。用户通过选一个图片,然后按submit就可以存入数据库中,其中先要建立一个表:

create table test
( name varchar(20),
pic blob );
在MySQL的test库中
<%@ page language="java"%>
<%@ taglib uri=
"http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri=
"http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<html>
<head>
 <title>JSP for multiForm form</title>
</head>
<body>
 <html:form action="/multi"
 enctype="multipart/form-data">
 一定要用enctype=“multipart/form-data“
 不然就提交之后就会有抛出异常
  file : <html:file property="file"/>
  <html:errors property="file"/></br>
  name : <html:text property="name"/>
  <html:errors property="name"/></br>
  <html:submit/><html:cancel/>
 </html:form>
</body>
</html>

  2. 相对应的ActionForm:

//Created by MyEclipse Struts
// XSL source (default):
platform:/plugin/com.genuitec.eclipse.
cross.easystruts.eclipse_3.8.1
/xslt/JavaClass.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:

责编:豆豆技术应用

正在加载评论...