目录
- 1.准备工作
- 2. 配置本地资源映射路径 addResourceHandlers
- 3.上传代码
1.准备工作
1、新建一个SpringBoot项目加上web依赖, 所有依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring5artifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
2、application.properties
# 上传文件总的最大值
spring.servlet.multipart.max-request-size=10MB
#spring.servlet.multipart.max-request-size=10KB
# 单个文件的最大值
spring.servlet.multipart.max-file-size=10MB
#spring.servlet.multipart.max-file-size=10KB
# 文件上传路径, 当前项目根目录下的 uploadFile 目录
pro.uploadPath=uploadFile
3、新建文件上传的静态文件 index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传title>
head>
<body>
<h1>文件上传h1>
<form method="post" action="/file/upload" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="提交">
form>
body>
html>
2. 配置本地资源映射路径 addResourceHandlers
package com.pro.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConf implements WebMvcConfigurer {
@Value("${pro.uploadPath}")
private String uploadPath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(uploadPath + "/**")
// file: 当前项目根目录, 映射到真实的路径下的 uploadPath(变量)目录下
.addResourceLocations("file:" + uploadPath + "/");
}
}
3.上传代码
package com.pro.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
public class UploadController {
@Value("${pro.uploadPath}")
private String uploadPath;
public String getUploadPath(HttpServletRequest request, String fileName) {
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + uploadPath + "/" + fileName;
}
@RequestMapping("/file/upload")
public Object upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
Map<String, Object> map = new HashMap();
map.put("msg", "上传成功");
if (multipartFile.isEmpty()) {
map.put("msg", "空文件");
return map;
}
String fileName = multipartFile.getOriginalFilename();
if ("".equals(fileName)) {
map.put("msg", "文件名不能为空");
return map;
}
System.out.println("上传文件原始的名字: " + fileName);
String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + fileName.substring(fileName.lastIndexOf("."), fileName.length());
System.out.println("保存的文件的新名字: " + newFileName);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String format = simpleDateFormat.format(new Date());
File readPath = new File(uploadPath + File.separator + format);
System.out.println("存放的文件夹: " + readPath);
System.out.println("存放文件的绝对路径: " + readPath.getAbsolutePath());
if (!readPath.isDirectory()) {
readPath.mkdirs();
}
File file = new File(readPath.getAbsolutePath() + File.separator + newFileName);
try {
multipartFile.transferTo(file);
String filePath = getUploadPath(request, format + "/" + newFileName);
map.put("path", filePath);
} catch (IOException e) {
e.printStackTrace();
map.put("msg", "上传失败");
}
return map;
}
}