用JBuilder2005开发spring MVC应用-multipart (fileupload)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

用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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航