前言:使用spring、springmvc整合的web项目,maven添加jar包依赖
1、依赖jar包配置,可手工导入进项目
2、spring、springmvc配置文件
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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!-- 开启自动扫包 spring无需关注@Controller注解的JAVA类-->
<context:component-scan base-package="test.service">
</context:component-scan>
<!-- 定时任务注解开启-->
<task:annotation-driven />
</beans>
springmvc.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置springmvc要扫描的注解-->
<context:component-scan base-package="test.controller" use-default-filters="false">
<!-- 配置白名单 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 启动AOP支持 激活自动代理功能 -->
<aop:aspectj-autoproxy/>
<context:annotation-config/>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<!-- 启用fastjson的json格式化输出、日期格式化输出 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="features">
<array>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
<value>WriteDateUseDateFormat</value>
<value>WriteNullStringAsEmpty</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置JSP视图 -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
<property name="order" value="1"/>
</bean>
<!-- 配置文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="209715200" />
<property name="resolveLazily" value="true" />
<property name="maxInMemorySize" value="4096" />
</bean>
</beans>
3、web.xml配置文件
<!-- 声明应用范围(整个WEB项目)内的上下文初始化参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>test.root</param-value>
</context-param>
<!-- 编码过滤 -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring容器监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- request监听 -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- springmvc Servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<!--使用spring进行拦截所有请求-->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
4、上传controller
@Controller
public class UploadController {
@Autowired
private UploadService uploadService;
@RequestMapping(value="/file/uplaod",method=RequestMethod.POST)
public String uploadFile(ModelMap model,MultipartFile file){
uploadService.uploadFile(file);
System.out.println("进入"+file.getOriginalFilename());
model.put("message", "上传成功");
return "common/message";
}
@RequestMapping(value="/filemulti/uplaod")
public String uploadMultiFile(ModelMap model,@RequestParam("files")MultipartFile[] files){
uploadService.uploadMultiFile(files);
model.put("message", "上传成功");
return "common/message";
}
}
5、上传service
@Service
public class UploadService {
public void uploadFile(MultipartFile file){
//this.getClass().getClassLoader().getResource("/").getPath();
String basePath = "D:/upload/";
File pathFile = new File(basePath);
if(!pathFile.exists()){
pathFile.mkdir();
}
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = basePath+file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void uploadMultiFile(MultipartFile[] files){
//判断file数组不能为空并且长度大于0
if(files!=null&&files.length>0){
//循环获取file数组中得文件
for(int i = 0;i<files.length;i++){
MultipartFile file = files[i];
//保存文件
uploadFile(file);
}
}
}
}
结语:
文件上传重要配置:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="209715200" />
<property name="resolveLazily" value="true" />
<property name="maxInMemorySize" value="4096" />
</bean>
MultipartFile用于接收单个文件,MultipartFile[]用于批量接收文件
参考文章:
fjsnail:http://www.cnblogs.com/fjsnail/p/3491033.html
SwingLife的专栏:http://blog.youkuaiyun.com/swingpyzf/article/details/20230865