一、导包
链接:https://pan.baidu.com/s/1mujHhWJF88lbQgr-XEA9gw
提取码:lclc
二、配置
注意:这里的id名只能是multipartResolver
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 数据验证 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator "></property>
</bean>
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
<!-- 在本配置文件中不要扫描service层和dao层 -->
<context:component-scan base-package="com.java.demo1.controller,com.java.exception,com.java.upload"></context:component-scan>
<!-- 扫描springmvc中的注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器(只对转发有效,重定向无效) -->
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置文件上传配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置文件上传的编码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 配置文件上传大小 (单位:字节)-->
<property name="maxUploadSize" value="1048576"></property>
</bean>
</beans>
三、编写代码
1)、单文件上传
1、Jsp
如果表单中有file控件,必须指定enctype=“multipart/form-data”
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="upFile">
<input type="submit" value="上传">
</form>
</body>
</html>
2、Servlet
package com.java.upload;
import java.io.File;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
/*
* 文件的上传要注意重名的问题(多个用户上传同一名字的文件时):
* 1、为不同的用户创建不同的文件夹
* 2、用算法达到不重名的效果(可以自己写,也可以用自带的算法工具,比如: UUID)
*/
@Controller //普通类被用Controller注释就变成了servlet
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class Upload {
@RequestMapping("upload.action")
public String upload(@RequestParam(name="upFile") MultipartFile file,HttpServletRequest request) {
//拿到要存放文件的文件夹路径(将相对路径转换成绝对路径)
String path = request.getServletContext().getRealPath("/upload");
System.out.println(path);
//拿到上传文件的名字
String filename = file.getOriginalFilename();
//xxx.jsp 拿到"."索引
int lastIndexOf = filename.lastIndexOf(".");
//xxx.jsp 获取到.jsp
String suffix = filename.substring(lastIndexOf);
//获取uuid,作为不重复的文件名
String uuid = UUID.randomUUID().toString();
//将上传的文件保存到指定目录下
File f = new File(path+File.separator+uuid+suffix);
try {
//将file对象中保存的文件转移到指定目录的文件夹下
file.transferTo(f);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "redirect:/upload.jsp";
}
}
2)、多文件上传
1、Jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%-- <form action="${pageContext.request.contextPath }/upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="upFile">
<input type="submit" value="上传">
</form> --%>
<form action="${pageContext.request.contextPath }/upload2.action" method="post" enctype="multipart/form-data">
<input type="file" name="files1">
<input type="file" name="files2">
<input type="file" name="files3">
<input type="submit" value="上传">
</form>
</body>
</html>
1、Servlet
package com.java.upload;
import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
/*
* 文件的上传要注意重名的问题(多个用户上传同一名字的文件时):
* 1、为不同的用户创建不同的文件夹
* 2、用算法达到不重名的效果(可以自己写,也可以用自带的算法工具,比如: UUID)
*/
@Controller //普通类被用Controller注释就变成了servlet
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class Upload {
@RequestMapping("upload2.action")
public String upload2(MultipartRequest files,HttpServletRequest request) {
//将上传的文件形成map集合
Map<String, MultipartFile> fileMap = files.getFileMap();
//拿到key形成set集合(因为set集合不重复)
Set<String> keySet = fileMap.keySet();
for (String key : keySet) {
//拿到其中一个文件(上面是拿到所有文件)
MultipartFile file = fileMap.get(key);
//拿到上传文件的名字
String filename = file.getOriginalFilename();
int lastIndexOf = filename.lastIndexOf(".");
String suffix = filename.substring(lastIndexOf);
//获取uuid,作为不重复的文件名
String uuid = UUID.randomUUID().toString();
//拿到要存放文件的文件夹路径(将相对路径转换成绝对路径)
String path = request.getServletContext().getRealPath("/upload");
System.out.println(path);
//将上传的文件保存到指定目录下
File f = new File(path+File.separator+uuid+suffix);
try {
//将file对象中保存的文件转移到指定目录的文件夹下
file.transferTo(f);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "redirect:/upload.jsp";
}
}