运行环境:tomcat5.0.30+springframework
步骤:
1。定义一个form表单index.
jsp和error.
jsp**************************************index.
jsp*************************************************
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="up.htm" enctype="multipart/form-data">
<input type="file" name="file"/><br>
<input type="file" name="file1"/><br>
<TEXTAREA name="description" ROWS=5 COLS=30>
</TEXTAREA><br>
<input type="submit"/>
</form>
</body>
</html>
**************************************error.
jsp**********************************
<%@ page language="
java" isErrorPage="true" %>
<head><title>Doh!</title></head>
An Error has occurred in this application.
2。定义控制类FileUploadController和commandClass UploadBean
**************************FileUploadController.
java*************************************************************
public class FileUploadController extends SimpleFormController {
private static Log log = LogFactory.getLog(getClass());
public ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse res, Object obj, BindException errors)
throws Exception {
UploadBean bean = (UploadBean) obj;
if(bean == null)return new ModelAndView("error");
byte[] file = bean.getFile();
byte[] file1 = bean.getFile1();
String[] d= bean.getDescription();
==================================================
在这里就可以对上面得到的数据进行处理,可以把上传的文件内容保存到本地或
者数据库,对文本区的说明文字也可以进行处理。其最大的优点就是把request里面
的数据绑定到了commandClass,有利于使用spring的验证机制和其他的辅助功能。
==================================================
}
//注册一个spring的编辑器非常重要,没有这个方法,上传将不能进行
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
}
****************************************UploadBean.
java***********************************
public class UploadBean {
private String[] description;
private byte[] file;
private byte[] file1;
public void setDescription(String[] description) {
this.description = description;
}
public String[] getDescription() {
return this.description;
}
public void setFile(byte[] file) {
this.file = file;
}
public byte[] getFile() {
return file;
}
public void setFile1(byte[] file1){
this.file1 = file1;
}
public byte[] getFile1(){
return file1;
}
}
================================================