目录
快速搭建springmvc框架省略,前面的文章有写。
导入坐标
文件上传需要的jar包坐标依赖,apache提供的
<!--文件上传的包,apache提供的-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
实现上传的三个条件
1、enctype="multipart/form-data",进行数据划分:表单项,文件项
2、method="post"
3、input类型为file
普通servlet方式上传
jsp页面
<%--
Created by IntelliJ IDEA.
User: Mocar
Date: 2019/9/13
Time: 3:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h3>文件上传</h3><br><br>
<form action="user/uploadFiles1" method="post" enctype="multipart/form-data" >
<input type="file" name="file"/><br>
<input type="submit" value="上传">
</form>
</body>
</html>
controller处理请求执行上传
/**
* @Date 2019/9/13 3:13
* by mocar
*/
@Controller
@RequestMapping("/user")
public class UserController {
/*传统表单上传*/
@RequestMapping("/uploadFiles1")
public String uploadFiles1(HttpServletRequest request) throws Exception {
System.out.println("uploadFiles1.....");
//获取部署目录,tomcat部署
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
System.out.println(realPath);
File file = new File(realPath);
if (file.exists()==false){//判断文件目录是否存在
file.mkdirs();//创建上传目录
}
//解析request对象,获取上传文件项
FileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
List<FileItem> fileItems = servletFileUpload.parseRequest(request);//获取到上传的文件项
for (FileItem fileItem : fileItems) {
//判断是否为上传的文件项
if (fileItem.isFormField()){
//表单项
}else {
String filename = fileItem.getName();//获取上传文件的名字
System.out.println(filename);
String uuid = UUID.randomUUID().toString().replace("-", "");//uuid生成随机字符
filename=uuid +"_" +filename;
//上传文件
fileItem.write(new File(realPath,filename));
//删除内存缓存文件 <10kb
fileItem.delete();
}
}
return "success";
}
}
realpath
基于spirngmvc文件上传
springmvc文件上传原理解析
细节1:方法入参名称必须和jsp的上传控件名称一致
细节2:配置的文件解析器bean,id必须为multipartResolver
配置文件解析器
<!--设置文件上传最大值 10M-->
<property name="maxUploadSize" value="10*1024*1024"></property>
这么写报错
正确配置
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启组件扫描,视图解析器,返回的jsp文件配置-->
<context:component-scan base-package="com.itcast"></context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--配置文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置文件上传最大值10M 注:10*1024*1024会报格式错误=10485760-->
<property name="maxUploadSize" value="10485760"></property>
<!--<property name="defaultEncoding" value="UTF-8"></property>
<property name="resolveLazily" value="false"></property>-->
</bean>
<!--前端控制器,哪些静态资源不拦截 location和mapping内容不要写反-->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!--开启springmvc框架的支持,即springmvc注解驱动开启-->
<mvc:annotation-driven ></mvc:annotation-driven>
</beans>
controller
/*springmvc上传*/
@RequestMapping("/uploadFiles2")
public String uploadFiles2(HttpServletRequest request, MultipartFile upload) throws Exception {
System.out.println("uploadFiles2.....");
//获取部署目录,tomcat部署
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
System.out.println(realPath);
File file = new File(realPath);
if (file.exists()==false){//判断文件目录是否存在
file.mkdirs();//创建上传目录
}
//解析request对象,获取上传文件项
//直接操作文件项
String filename = upload.getOriginalFilename();//获取文件名称
String uuid = UUID.randomUUID().toString().replace("-", "");
filename=uuid+"_"+filename;
upload.transferTo(new File(realPath,filename));//上传文件
//springmvc帮删除内存缓存文件
return "success";
}
realpath: