一、pom.xml 文件依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
<!--web支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat 的支持.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--redis缓存依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
二、jsp页面
<%--
Created by IntelliJ IDEA.
Date: 2020/9/23
Time: 9:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上传图片</title>
</head>
<body>
<form action="/imageUpload" method="post" enctype="multipart/form-data">
<label>上传图片</label>
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
<p>图片:</p>
<img src="${fileName}"/>
</body>
</html>
**注意:**form 表单的 action="/imageUpload" 属性,要和controller 的postMaping("")注解的映射地址 一样。
三、Controller控制类
/**
*@ProjectName: demo
* @Package: com.example.controller
* @ClassName: ImageController
* @Description: 图片上传
*/
@Controller
public class UploadController {
/**
* @Method index
* @Version 1.0
* @Description
* @param
* @Return Srting
*/
@RequestMapping(value = "/index")
public String index(){
//用于返回页面显示
return "index";
};
/**
* @Method uploadImage
* @Version 1.0
* @Description
* @param file
* @Return String
*/
@PostMapping("/imageUpload")
public String uploadImage(HttpServletRequest req, @RequestParam("file")MultipartFile file, Model m){
if (file.isEmpty()) {
System.out.println("文件为空!");
}
//获取文件名称
String fileName = file.getOriginalFilename();
//获取文件拓展名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//设置文件保存
String localPath = "D://image//";
fileName = UUID.randomUUID() + suffixName;
//判断是否上传成功
if(FileUntil.upload(file, localPath, fileName)){
fileName = "/image/" + fileName;
//把文件名放在model里,以便后续显示用
m.addAttribute("fileName", fileName);
} ;
return "index";
};
}
四,创建工具类
/**
* @ProjectName: demo
* @Package: com.example.until
* @ClassName: UploadUntil
* @Description: 上传工具类
*/
public class FileUntil {
/**
* @Method upload
* @Version 1.0
* @Description
* @param file
* @param path
* @param fileName
* @Return boolean
*/
public static boolean upload(MultipartFile file, String path, String fileName) {
//确定文件真实路径
String realPath = path + "\\" + fileName;
System.out.println("上传文件:"+ realPath);
File dest = new File(realPath);
//判断父文件是否存在
if (!dest.getParentFile().exists()){
dest.getParentFile().mkdir();
}
try {
//将上传文件写到服务器上指定的文件
file.transferTo(dest);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
};
}
五、配置资源映射地址
/**
* @ProjectName: demo
* @Package: com.example.config
* @ClassName: MyWebMvcConfigurer
* @Description: 上传文件配置资源映射地址
*/
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/**").addResourceLocations("file:D:/image/");
}
}
注意:如果上传的文件的是保存在本地磁盘中,必须要配置资源映射地址,不然图片无法被获 取,图片无法被展示。