这个例子是通过用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:
//created by myeclipse struts
// xsl source (default):
platform:/plugin/com.genuitec.eclipse.
cross.easystruts.eclipse_3.8.1
/xslt/javaclass.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");
}
}
|