用JBuilder2005开发spring MVC应用-multipart (fileupload)
高科华作者简介:高科华,南京航空学院计算数学专业硕士,有十年以上的企业信息化工作经验。目前的研究兴趣,J2EE企业应用、ERP软件研发、数据仓库系统研发。
1. 按照“用JBuilder2005开发spring MVC应用”一文建立基本的spring应用2. 增加commons-fileupload.jar类库
3. 增加两个类文件
/src/fileupload/ FileUploadController.java
package fileupload;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.validation.*;
import org.springframework.web.bind.*;
import org.springframework.web.multipart.support.*;
import org.springframework.web.servlet.*;
import org.springframework.web.servlet.mvc.*;
// snippet from FileUploadController
public class FileUploadController extends SimpleFormController {
protected ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws ServletException, IOException {
// cast the bean
FileUploadBean bean = (FileUploadBean) command;
// let's see if there's content there
byte[] file = bean.getFile();
if (file.length == 0) {
// hmm, that's strange, the user did not upload anything
return new ModelAndView(this.getFormView());
}
// well, let's do nothing with the bean for now and return:
File myFile = new File("myfile.txt");
myFile.createNewFile();
try {
return super.onSubmit(request, response, command, errors);
} catch (Exception ex) {
return null;
}
}
protected void initBinder(
HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
// to actually be able to convert Multipart instance to byte[]
// we have to register a custom editor (in this case the
// ByteArrayMultipartEditor
binder.registerCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}
}
上载文件时如果选定了要上载的文件,file.length就不为0,否则file.length为0。上载文件的内容保存在byte[] file中,本例子没有对上载的文件进行处理。
/web-inf/fileupload/FileUploadBean.java
package fileupload;
// snippet from FileUploadBean
public class FileUploadBean {
private byte[] file;
public void setFile(byte[] file) {
this.file = file;
}
public byte[] getFile() {
return file;
}
}
4. Springapp-servlet.xml文件如下:
/web-inf/springapp-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="HomePageController" class="xslt.HomePageController"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.htm">HomePageController</prop>
<prop key="/uploadfile.htm">fileUploadController</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="fileUploadController" class="fileupload.FileUploadController">
<property name="commandClass">
<value>fileupload.FileUploadBean</value>
</property>
<property name="formView">
<value>fileuploadform</value>
</property>
<property name="successView">
<value>confirmation</value>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize">
<value>100000</value>
</property>
</bean>
</beans>
属性successView的值confirmation指的是confirmation.jsp。InternalResourceViewResolver使我们可以省略前缀(文件的路径)和后缀(.jsp)
5. 文件Confirmation.jsp是上载文件成功后的确认页面
/web-inf/jsp/confirmation.jsp
<%@ page contentType="text/html; charset=Big5" %>
<html>
<head>
<title>
successView
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
successView
</h1>
</body>
</html>
6. 如果用户没有选择上载的文件就提交表单,文件fileuploadform.jsp就提示用户选择上载的文件后再提交
/web-inf/jsp/fileuploadform.jsp
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>no file,Please upload a file</h1>
<form method="post" action="uploadfile.htm" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
7. 文件upload.jsp把表单的处理交给uploadfile.htm(不知为什么,用uploadfile.form不行),springapp-servlet.xml把uploadfile.xml映照到fileUploadController类。
Springapp/web-inf/jsp/upload.jsp
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="uploadfile.htm" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
8. 浏览http://localhost:8080/springapp/upload.jsp
本文地址:http://com.8s8s.com/it/it12183.htm