日期:2014-05-16  浏览次数:20349 次

用struts向数据库中储存图片
这个例子是通过用Struts的FormFile来写入到MySQL中。。。

用用户通过选一个图片,然后按submit就可以存入数据库中

其中先要建立一个表:

create table test ( name varchar(20), pic blob );在MySQL的test库中

  1<%@ page language="java"%>
  2<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
  3<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
  4
  5<html>
  6 <head>
  7  <title>JSP for multiForm form</title>
  8 </head>
  9 <body>
10  <html:form action="/multi" enctype="multipart/form-data">一定要用enctype=“multipart/form-data“不然就提交之后就会有抛出异常
11   file : <html:file property="file"/><html:errors property="file"/></br>
12   name : <html:text property="name"/><html:errors property="name"/></br>
13   <html:submit/><html:cancel/>
14  </html:form>
15 </body>
16</html>
17
182. 相对应的ActionForm:
19
20  //Created by MyEclipse Struts
21  // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.1/xslt/JavaClass.xsl
22
23  package saoo.struts.form;
24
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.upload.FormFile;
27
28/** *//**
29 * MyEclipse Struts
30 * Creation date: 08-24-2004
31 *
32 * XDoclet definition:
33 * @struts:form name="multiForm"
34 */
35public class MultiForm extends ActionForm {
36
37    // --------------------- Instance Variables
38
39    /** *//** file property */
40    private FormFile file;
41
42    /** *//** name property */
43    private String name;
44
45    // --------------------- Methods
46
47    /** *//**
48     * Returns the file.
49     * @return FormFile
50     */
51    public FormFile getFile() {
52        return file;
53    }
54
55    /** *//**
56     * Set the file.
57     * @param file The file to set
58     */
59    public void setFile(FormFile file) {
60        this.file = file;
61    }
62
63    /** *//**
64     * Returns the name.
65     * @return String
66     */
67    public String getName() {
68        return name;
69    }
70
71    /** *//**
72     * Set the name.
73     * @param name The name to set
74     */
75    public void setName(String name) {
76        this.name = name;
77    }
78}
79
803. 对就的Action:
81
82  //Created by MyEclipse Struts
83  // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.1/xslt/JavaClass.xsl
84
85  package saoo.struts.action;
86
87  import java.io.FileNotFoundException;
88  import java.io.IOException;
89  import java.sql.Connection;
90  import java.sql.DriverManager;
91  im