1.简介
基本知识点就是在表单中使用file,进行文件的提交,然后在Controller中使用Multipartfile跟part进行文件的处理,上传。
2.part跟Multipartfile也有不同点:
part是servlet提供的,路径自己写
multipartFile是spring提供的,路径自行配置
Part获取原始文件名: getSubmittedFileName();
MultipartFile获取原始文件名是:getOriginalFileName();
- 音乐类型文件无法进行上传 自测!
4.单文件上传
MultipartFile之springMVC方法的单文件上传:
(1)multipartfile在web.xml中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Spring IoC容器加载的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 用于初始化Spring IoC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ds</servlet-name>
<!-- 核心控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 应用启动时,加载servlet -->
<load-on-startup>1</load-on-startup>
<multipart-config>
<location>e:/</location>
<!-- 文件的最大大小,单位为字节 -->
<max-file-size>5242880</max-file-size>
<!-- 请求的最大大小,单位为字节 -->
<max-request-size>10485760</max-request-size>
<!--文件大小阈值,当大于这个阈值时将写入到磁盘,否则在内存中。默认值为0-->
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<!-- 约定优于配置, Spring MVC框架默认加载/WEB-INF/<servlet-name/>开头-servlet.xml作为配置文件载入Web工程中 -->
<servlet-name>ds</servlet-name>
<!-- 拦截配置 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<multipart-config>
<location>e:/</location>
<!-- 文件的最大大小,单位为字节 -->
<max-file-size>5242880</max-file-size>
<!-- 请求的最大大小,单位为字节 -->
<max-request-size>10485760</max-request-size>
<!--文件大小阈值(相当于缓冲区),当大于这个阈值时将写入到磁盘,否则在内存中。默认值为0-->
<file-size-threshold>0</file-size-threshold>
</multipart-config>
(2)form表单:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="file/uploadpart.do" method="post" enctype="multipart/form-data">
<input type="text" name="uname"/>
<input type="text" name="note"/>
<input type="file" name="photo"/>
<!-- <input type="file" name="photo"/> -->
<input type="submit" value="提交"/>
</form>
</body>
</html>
(3)Controller控制器
package com.gb.controller;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.Part;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;
import com.gb.pojo.UserRegister;
@Controller
@RequestMapping("file")
public class FileController {
@RequestMapping("upload")
public ModelAndView upload(MultipartFile photo, String uname, String note) {
String fileName = photo.getOriginalFilename();
String contentType = photo.getContentType();
String destFile = UUID.randomUUID() + fileName;
File file = new File(destFile);
ModelAndView mv = new ModelAndView();
try {
photo.transferTo(file);
mv.addObject("success","上传成功");
} catch (IllegalStateException | IOException e) {
mv.addObject("fail","上传失败");
e.printStackTrace();
}
mv.setView(new MappingJackson2JsonView());
return mv;
}
(4)如果参数比较多的话可以使用pojo进行参数的传递(设置原型避免单例)
package com.gb.pojo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope(scopeName="prototype")
public class UserRegister {
private String uname;
private String note;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}}
@RequestMapping("file")
public ModelAndView file(MultipartFile photo, UserRegister user) {
String fileName = photo.getOriginalFilename();
String contentType = photo.getContentType();
String destFile = UUID.randomUUID() + fileName;
File file = new File(destFile);
ModelAndView mv = new ModelAndView();
try {
photo.transferTo(file);
mv.addObject("success","上传成功");
} catch (IllegalStateException | IOException e) {
mv.addObject("fail","上传成功");
e.printStackTrace();
}
mv.setView(new MappingJackson2JsonView());
return mv;
}
单文件上传之part形式:
part是servlet提供的,路径不需要配置的,所以也不用在web.xml配置文件中进行路径以及阈值等的配置
Controller控制器中的代码:
@RequestMapping("uploadpart")
public ModelAndView upload(Part photo, String uname, String note) {
// 获取源文件名
String fileName = photo.getSubmittedFileName();
String contentType = photo.getContentType();
String destFile = UUID.randomUUID() + fileName;
ModelAndView mv = new ModelAndView();
try {
photo.write("f:/" + destFile);
mv.addObject("success","上传成功");
} catch (IllegalStateException | IOException e) {
mv.addObject("fail","上传失败");
e.printStackTrace();
}
mv.setView(new MappingJackson2JsonView());
return mv;
}
这里使用的是 getSubmittedFileName()方法来获取的源文件名,但是这个方法在tomcat7.x中是没有的。
5.多文件上传
使用Multipartfile数组形式
@RequestMapping("uploadmulti")
public ModelAndView uploadMulti(MultipartFile[] photo, UserRegister ur) {
ModelAndView mv = new ModelAndView();
try {
for(MultipartFile mf:photo) {
String destFile = UUID.randomUUID() + mf.getOriginalFilename();
File file = new File(destFile);
mf.transferTo(file);
}
mv.addObject("success","上传成功");
} catch (IllegalStateException | IOException e) {
mv.addObject("fail","上传失败");
e.printStackTrace();
}
mv.setView(new MappingJackson2JsonView());
return mv;
}
原理很简单,就是form表单中加入多个file,提交的时候提交多个文件到Multipartfile数组中,再在控制器中进行foreach遍历添加UUID路径等处理,最后transferTo发送一下就ok了。
也可以使用part数组形式。。。