This example is written in MySQL by using Struts's Formfile. . .
Use the user by selecting a picture, then press SUBMIT to deposit into the database
First set up a table:
Create Table Test (Name Varchar (20), Pic Blob); in MySQL Test Library
<% @ 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 "%>
2. Comparison 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. For the Action:
// Created by myeclipse struts // xsl source (default): platform: /plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.1/xslt/javaclass.xsl
Package Sao.struts.Art;
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 Sao.Struts.form.Multiform;
/ ** * myeclipse struts * code date: 08-24-2004 * xdoclet definition: * @struts: action path = "/ multi" name = "multiform" infut = "/ 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); // Add a picture to the database ps.setbinaryStream (2, file.getinputstream (), file.GetFileSize (); ps.ExecuteUpdate (); ps.close (); conif.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");}
}