在开发过程中肯定遇到很多问题,比如上传方案完全没问题 就是上小的文件(10K左右的文件)无法上传?或者文件(好几百兆)大了点也不好使?
其实这只是因为不了解默认特性,这个是有默认值的,这个在MVC配置中可以解决。
直接贴出全部配置 解说在最后面,建议按需配置 :
配置文件spring4mvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 启用spring mvc 注解 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<!-- <value>text/html;charset=UTF-8</value> --> <!-- 避免IE出现下载JSON文件的情况 -->
</list>
</property>
<property name="features">
<array>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="DisableCircularReferenceDetect" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"></property>
</bean>
<!-- 设置跨域不限制-->
<mvc:cors>
<mvc:mapping path="/**" />
</mvc:cors>
<!-- 对静态资源文件的访问 缓存一年 <mvc:resources mapping="/images/**" location="/WEB-INF/images/"
cache-period="31536000"/> <mvc:resources mapping="/css/**" location="/WEB-INF/css/"
/> <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> <mvc:resources
mapping="/fonts/**" location="/WEB-INF/fonts/" /> <mvc:resources mapping="/favicon.ico"
location="favicon.ico" /> -->
<!-- 自动扫描的包名 ,使Spring支持自动检测组件,如注解的Controller -->
<context:component-scan base-package="cc.javar.controller" />
<context:component-scan base-package="cc.javar.service" />
<!-- 视图解析器:定义跳转的文件的前后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" /> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="order" value="1" />
</bean>
<!--<mvc:view-controller path="/" view-name="forward:/index.jsp"/> -->
<!-- 缓存配置(两种) -->
<!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->
<!-- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches"> <set> <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set> </property> </bean> -->
<!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->
<!-- Spring提供的基于的Ehcache实现的缓存管理器 -->
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory" />
</bean>
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/>
<!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
<property name="maxUploadSize" value="112400000"/>
<!-- 指定所上传文件的最小单位为10字节 -->
<property name="maxInMemorySize" value="10" />
</bean>
</beans>