這個例子是通過用Struts的FormFile來寫入到MySQL中
用用戶通過選一個圖片
其中先要建立一個表:
create table test ( name varchar(
<%@ page language=
<%@ taglib uri=
<%@ taglib uri=
<html>
<head>
<title>JSP for multiForm form</title>
</head>
<body>
<html:form action=
file : <html:file property=
name : <html:text property=
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com
package saoo
import org
import org
/**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts:form name=
*/
public class MultiForm extends ActionForm {
//
/** file property */
private FormFile file;
/** name property */
private String name;
//
/**
* 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
}
/**
* 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
}
}
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com
package saoo
import java
import java
import java
import java
import java
import java
import javax
import javax
import org
import org
import org
import org
import org
import saoo
/**
* MyEclipse Struts
* Creation date:
*
* XDoclet definition:
* @struts:action path=
*/
public class MultiAction extends Action {
//
//
/**
* 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
String name = multiForm
try {
Class
String url=
Connection con=DriverManager
String sql=
PreparedStatement ps =con
ps
//加入圖片到數據庫
ps
ps
ps
con
} catch (SQLException se) {
se
return mapping
} catch (ClassNotFoundException e) {
// TODO Auto
e
return mapping
} catch (FileNotFoundException e) {
// TODO Auto
e
return mapping
} catch (IOException e) {
// TODO Auto
e
return mapping
}
return mapping
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27905.html