日期:2014-05-16  浏览次数:20371 次

SpringMVC结合ajaxfileupload.js实现文件无刷新上传

直接看代码吧,注释都在里面


首先是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
		<description>配置SpringMVC的前端控制器</description>
		<servlet-name>upload</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>upload</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<filter>
		<description>解决参数传递过程中的乱码问题</description>
		<filter-name>CharacterEncodingUTF8</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingUTF8</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

下面是位于//src//applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
						http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	<!-- 启动Spring的组件自动扫描机制(Spring会自动扫描base-package指定的包中的类和子包里面类) -->
	<!-- 此处可参考我的文章http://blog.csdn.net/jadyer/article/details/6038604 -->
	<context:component-scan base-package="com.jadyer"/>
	
	<!-- 启动SpringMVC的注解功能,它会自动注册HandlerMapping、HandlerAdapter、ExceptionResolver的相关实例 -->
	<mvc:annotation-driven/>
	
	<!-- 由于web.xml中设置SpringMVC拦截所有请求,所以在读取静态资源文件时就会读不到 -->
	<!-- 通过此配置即可指定所有请求或引用"/js/**"的资源,都会从"/js/"中查找 -->
	<mvc:resources mapping="/js/**" location="/js/"/>
	<mvc:resources mapping="/upload/**" location="/upload/"/>
	
	<!-- SpringMVC上传文件时,需配置MultipartResolver处理器 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 指定所上传文件的总大小不能超过800KB......注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
		<property name="maxUploadSize" value="800000"/>
	</bean>
	
	<!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException -->
	<!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 -->
	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.Sim