这个例子是通过用Struts的FormFile来写入到MySQL中
用用户通过选一个图片然后按submit就可以存入数据库中
其中先要建立一个表:
create table test ( name varchar() pic blob );在MySQL的test库中
<%@ page language=java%>
<%@ taglib uri=bean prefix=bean%>
<%@ taglib uri=html prefix=html%>
<html>
<head>
<title>JSP for multiForm form</title>
</head>
<body>
<html:form action=/multi enctype=multipart/formdata>一定要用enctype=multipart/formdata不然就提交之后就会有抛出异常
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>
相对应的ActionForm:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/comgenuitececlipsecrosseasystrutseclipse_/xslt/JavaClassxsl
package saoostrutsform;
import orgapachestrutsactionActionForm;
import orgapachestrutsuploadFormFile;
/**
* MyEclipse Struts
* Creation date:
*
* 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) {
thisfile = 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) {
thisname = name;
}
}
对就的Action:
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/comgenuitececlipsecrosseasystrutseclipse_/xslt/JavaClassxsl
package saoostrutsaction;
import javaioFileNotFoundException;
import javaioIOException;
import javasqlConnection;
import javasqlDriverManager;
import javasqlPreparedStatement;
import javasqlSQLException;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgapachestrutsactionAction;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;
import orgapachestrutsuploadFormFile;
import saoostrutsformMultiForm;
/**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts:action path=/multi name=multiForm input=/form/multijsp 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 = multiFormgetFile();
String name = multiFormgetName();
try {
ClassforName(orggjtmmmysqlDriver);
String url=jdbc:mysql:///test;
Connection con=DriverManagergetConnection(urlrootpassword);
String sql=insert into pic values (??);
PreparedStatement ps =conprepareStatement(sql);
pssetString( name);
//加入图片到数据库
pssetBinaryStream(filegetInputStream()filegetFileSize());
psexecuteUpdate();
psclose();
conclose();
} catch (SQLException se) {
seprintStackTrace();
return mappingfindForward(error);
} catch (ClassNotFoundException e) {
// TODO Autogenerated catch block
eprintStackTrace();
return mappingfindForward(error);
} catch (FileNotFoundException e) {
// TODO Autogenerated catch block
eprintStackTrace();
return mappingfindForward(error);
} catch (IOException e) {
// TODO Autogenerated catch block
eprintStackTrace();
return mappingfindForward(error);
}
return mappingfindForward(success);
}
}