运行环境: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;
}
}
================================================
3.配置web.xml文件
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
4。配置simple-servlet.xml文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/up.htm">simpleController</prop>
</props>
</property>
</bean>
<bean id="simpleController" class="SimpleController">
<property name="commandClass"><value>UploadBean</value></property>
</bean>
</beans>
5.配置tomcat,添加所需的jar文件,commons-logging.jar;servlet-api.jar;spring.jar;jstl.jar
启动tomcat,预览:http://localhost:8080/(根目录名)/index.jsp
如果有问题,请参照前面我blog里面关于单个文件上传的文章。
说明几点:
a。表单里面的name值,必须和commandClass里面的byte[]的名称相对应。
b. 控制类里面一定要注册一个编辑器。如果没有initBinder()则上传不能进行。
c。发现spring一个问题,如果上传的文件过大(超过30M),会抛出一个
java.lang.OutOfMemoryError: Java heap space
异常,现在不知道怎么解决。
d。spring上传的速度一般维持在两百多k,不知道有没有办法可以突破这个瓶颈?
e。希望有经验的XDJM可以和我探讨上面的c,d及spring有关的问题,可以给我留言,或发信luna8418@163.com