实现文件上传的思路:
- 创建form表单,enctype=“multipart/form-data(二进制)”,method=“post”,type=“file”。
- 开发控制器controller,控制器方法中的参数用:MultipartFile,进行文件的接收。
- 在SpringMVC配置文件中加入文件上传的解析器配置:CommonsMultipartReslover,id要严格写死:multipartReslover。
- 引入文件上传的相关依赖:commons-fileupload
具体代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form enctype="multipart/form-data" method="post"
action="${pageContext.request.contextPath}/file/update">
<input type="file" name="file"/>
<input type="submit" value="上传文件" />
</form>
</body>
</html>
@Controller()
@RequestMapping("/file")
public class FileController {
/**
* 用来处理文件上传
*/
@RequestMapping("/update")
public String update(MultipartFile file, HttpServletRequest request) throws IOException {
System.out.println("文件名: " + file.getOriginalFilename());
System.out.println("文件大小: " + file.getSize());
System.out.println("文件类型: " + file.getContentType());
// 文件上传
// 1.根据upload的相对路径,获取部署到服务器之后的绝对路径。
// request.getRealPath("/upload");过时
String realPath = request.getSession().getServletContext().getRealPath("/upload");
// 2.将文件上传到upload的绝对路径
file.transferTo(new File(realPath, file.getOriginalFilename()));
return "update";
}
}
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
文件上传的细节:
如何修改文件上传时文件的原始名称问题。
file.getOriginalFileName() ==> 获取的是原始文件名
问题:
直接使用文件的原始名称作为上传文件的最终名称,如果上传同名的文件,会出现同名文件覆盖问题。
解决方案:
每次用户上传文件,修改为一个新的名称。
新文件名:UUID + 文件后缀
为了便于对上传文件的管理,将用户每天上传的文件上传到当天日期目录中。
SpringMVC中如何解决文件上传的大小限制。
具体代码:
修改文件上传时文件的原始名称
@Controller()
@RequestMapping("/file")
public class FileController {
/**
* 用来处理文件上传
*/
@RequestMapping("/update")
public String update(MultipartFile file, HttpServletRequest request) throws IOException {
System.out.println("文件名: " + file.getOriginalFilename());
System.out.println("文件大小: " + file.getSize());
System.out.println("文件类型: " + file.getContentType());
// 文件上传
// 1.根据upload的相对路径,获取部署到服务器之后的绝对路径。
// request.getRealPath("/upload");过时
String realPath = request.getSession().getServletContext().getRealPath("/upload");
// 2.修改文件的原始名称
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String newFileName = UUID.randomUUID().toString() + "." + extension;
// 3.将文件上传到upload的绝对路径
file.transferTo(new File(realPath, newFileName));
return "update";
}
}
便于对上传文件的管理
@Controller()
@RequestMapping("/file")
public class FileController {
/**
* 用来处理文件上传
*/
@RequestMapping("/update")
public String update(MultipartFile file, HttpServletRequest request) throws IOException {
System.out.println("文件名: " + file.getOriginalFilename());
System.out.println("文件大小: " + file.getSize());
System.out.println("文件类型: " + file.getContentType());
// 文件上传
// 1.根据upload的相对路径,获取部署到服务器之后的绝对路径。
// request.getRealPath("/upload");过时
String realPath = request.getSession().getServletContext().getRealPath("/upload");
// 2.修改文件的原始名称
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String newFileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
// 3.生成当天的日期目录
LocalDate now = LocalDate.now();
File dateDir = new File(realPath, now.toString());
if (!dateDir.exists()) {
dateDir.mkdirs();
}
// 4.将文件上传到upload对应日期的目录
file.transferTo(new File(dateDir, newFileName));
return "update";
}
}
限制文件的上传大小
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 注入文件上传的大小 单位字节 20M = 20971520字节 默认没有限制 -->
<property name="maxUploadSize" value="20971520"/>
</bean>