Springboot实现图片上传功能的示例代码
作者:天海奈奈 时间:2022-08-05 22:46:44
首先对图片进行UUID 防止图片被覆盖以及爬图
UUID的生成规则:日期时间,MAC地址,HashCode,随机数(多种之一)
开发上传接口,两处红字意思是生成文件夹和生成目标文件,
/**
* 图片上传
* @param httpServletRequest 用来获取url
* @param file 上传的文件
* @return ApiRestResponse
*/
@PostMapping("/admin/upload/file")
public ApiRestResponse upload(HttpServletRequest httpServletRequest,
@RequestParam("file") MultipartFile file) {
//获取后缀
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
//生成文件名称UUID
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString() + suffixName;
//创建文件
File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);//文件夹
File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);//目标文件
if (!fileDirectory.exists()) {
if (!fileDirectory.mkdir()) {
throw new SpringLearnException(SpringLearnExceptionEnum.MKDIR_FAILED);
}
}
try {
file.transferTo(destFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
return ApiRestResponse
.success(getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/images/"
+ newFileName);
} catch (URISyntaxException e) {
return ApiRestResponse.error(SpringLearnExceptionEnum.UPLOAD_FAILED);
}
}
private URI getHost(URI uri) {
URI effectiveURI;
try {
effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
null, null, null);
} catch (URISyntaxException e) {
effectiveURI = null;
}
return effectiveURI;
}
由于生成文件时传入的时常量也就是我们生成文件的地址,所以需要去定义常量
/**
* 描述: 常量值
*
*/
@Component
public class Constant {
public static String FILE_UPLOAD_DIR;
@Value("${file.upload.dir}")
public void setFileUploadDir(String fileUploadDir) {
FILE_UPLOAD_DIR = fileUploadDir;
}
}
定义好之后去配置文件application.properties中书写具体保存在哪里[file.upload.dir]与@Vlue里面的值保持一致。
file.upload.dir=D:/pactures/
需要注意几个点
由于常量是static,我们直接将注解加载常量上方是无法找到我们定义的文件地址的
看到我们有一个地方是null,点击蓝字 可以看出确实是没找到我们的文件夹。
所以我们应当在下方新写一个方法去写常量的值,并将@Value("${file.upload.dir}")注解加在方法上方,并且我们需要在类上加@Component注解。
现在我们就能上传图片了,我们先去文件夹中看一下
文件生成功,我们再复制url去网页上看一下
这是因为它这个url地址不能映射到我们的本地目录,我们去配置SpringBootWebMvcConfig
就能打开了
/**
* 描述: 配置地址映射
*/
@Configuration
public class SpringLearnWebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**")
.addResourceLocations("file:" + Constant.FILE_UPLOAD_DIR);//把url地址映射到本地目录
}
}
来源:https://blog.csdn.net/weixin_57169969/article/details/126307196
标签:Springboot,图片,上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android开发ListView中下拉刷新上拉加载及带列的横向滚动实现方法
2023-06-16 01:09:52
![](https://img.aspxhome.com/file/2023/6/122126_0s.png)
C# Double转化为String时的保留位数及格式方式
2021-09-08 13:12:00
MyBatis中map的应用与模糊查询实现代码
2021-09-02 18:27:14
Compare And Swap底层原理及代码示例详解
2022-10-10 16:06:18
![](https://img.aspxhome.com/file/2023/8/125658_0s.png)
Springmvc ajax跨域请求处理方法实例详解
2023-08-25 22:56:46
WPF实现背景灯光随鼠标闪动效果
2023-09-05 15:39:27
![](https://img.aspxhome.com/file/2023/5/98175_0s.gif)
Android 无障碍服务 performAction 调用过程分析
2023-05-03 12:06:17
Java实现动态获取文件的绝对路径
2022-12-19 00:34:14
![](https://img.aspxhome.com/file/2023/2/61392_0s.png)
海量数据去重排序bitmap(位图法)在java中实现的两种方法
2022-10-10 17:27:36
springcloud使用Hystrix进行微服务降级管理
2023-02-02 06:51:30
![](https://img.aspxhome.com/file/2023/7/88557_0s.png)
磁盘配额的wmi版本(C#)
2022-07-31 17:55:17
c# chart缩放,局部放大问题
2021-09-26 20:46:40
![](https://img.aspxhome.com/file/2023/7/116527_0s.png)
Android中一种巧妙的drawable.xml替代方案分享
2021-12-06 04:18:35
![](https://img.aspxhome.com/file/2023/9/138829_0s.png)
Android基于TextView实现的跑马灯效果实例
2023-10-07 02:45:05
Java中对list map根据map某个key值进行排序的方法
2023-09-04 17:10:03
Redis监听过期的key实现流程详解
2023-12-12 02:41:28
![](https://img.aspxhome.com/file/2023/4/94324_0s.png)
Java实现简易生产者消费者模型过程解析
2023-12-03 01:33:26
![](https://img.aspxhome.com/file/2023/1/76261_0s.png)
Android App实现应用内部自动更新的最基本方法示例
2022-11-02 02:49:46
![](https://img.aspxhome.com/file/2023/6/138066_0s.png)
Android项目中引入aar包的正确方法介绍
2021-09-01 20:10:16
Java 异步实现的几种方式小结
2022-09-23 00:26:56
![](https://img.aspxhome.com/file/2023/9/71679_0s.png)