【SpringBoot学习】5、SpringBoot 实现文件上传,图片上传并显示功能
SpringBoot 实现文件上传,图片上传并显示功能#
我这里使用的是 springboot 2.0.3,不需要导入相关 jar 包,2.x 的版本已经整合进去了,直接使用即可。
spring 官网提供了 springboot 的文件上传下载案例,这是网址:https://spring.io/guides/gs/uploading-files/,使用的是流的输出。
下面的案例是 springboot2.x 图片上传与回显。我使用的工具是 idea。
1、创建 idea 默认的 springboot 项目,我的版本是 2.0.3
2、创建一个控制层 FileController
package com.rainy.controller;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.UUID;
/**
* 文件上传
*/
@Controller
public class FileController {
@GetMapping(value = "/file")
public String file() {
return "file";
}
@PostMapping(value = "/fileUpload")
public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) {
if (file.isEmpty()) {
System.out.println("文件为空空");
}
String fileName = file.getOriginalFilename(); // 文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
String filePath = "D://temp-rainy//"; // 上传后的路径
fileName = UUID.randomUUID() + suffixName; // 新文件名
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
String filename = "/temp-rainy/" + fileName;
model.addAttribute("filename", filename);
return "file";
}
}
3、创建 MyWebMvcConfigurer,这里是配置资源映射路径,详细点的介绍看这篇文章:https://blog.csdn.net/qq_38762237/article/details/81283241
/**
* 资源映射路径
*/
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/temp-rainy/**").addResourceLocations("file:D:/temp-rainy/");
}
}
4、jsp 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/fileUpload" 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>
注意一点:我是使用 jsp 引擎来渲染,因为我不会用 Thymeleaf,添加 jsp 页面,springboot 使用 jsp 页面是需要进行配置 jsp 整合的,默认的是 Thymeleaf 的页面,简单的就是 HTML 页面
springboot 配置 jsp 页面的方法:https://blog.csdn.net/qq_38762237/article/details/81283352
作者:mountainstudy
出处:https://www.cnblogs.com/mountainstudy/p/17604047.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫