gpt4 book ai didi

Spring Boot实现图片上传功能

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Spring Boot实现图片上传功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例为大家分享了Spring Boot图片上传的具体代码,供大家参考,具体内容如下 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.clou.inteface.domain.web.user;
 
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
/**
  * 文件上传
  * @author Fly
  *
  */
@RestController
public class FileUpload {
 
  /**
  * 用户管理 -> 业务层
  */
  @Autowired
  private SUserService sUserService;
 
  /**
  * 文件上传根目录(在Spring的application.yml的配置文件中配置):<br>
  * web:
  * upload-path: (jar包所在目录)/resources/static/
  */
  @Value ( "${web.upload-path}" )
  private String webUploadPath;
 
  /**
  * ResultVo是一个对象,包含:
  * private int errorCode;
  * private String errorMsg;
  * private Integer total;
  * private Object data;
  */
 
  /**
  * 基于用户标识的头像上传
  * @param file 图片
  * @param userId 用户标识
  * @return
  */
  @PostMapping (value = "/fileUpload" , consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResultVo fileUpload( @RequestParam ( "file" ) MultipartFile file, @RequestParam ( "userId" ) Integer userId) {
  ResultVo resultVo = new ResultVo();
  if (!file.isEmpty()) {
  if (file.getContentType().contains( "image" )) {
  try {
   String temp = "images" + File.separator + "upload" + File.separator;
   // 获取图片的文件名
   String fileName = file.getOriginalFilename();
   // 获取图片的扩展名
   String extensionName = StringUtils.substringAfter(fileName, "." );
   // 新的图片文件名 = 获取时间戳+"."图片扩展名
   String newFileName = String.valueOf(System.currentTimeMillis()) + "." + extensionName;
   // 数据库保存的目录
   String datdDirectory = temp.concat(String.valueOf(userId)).concat(File.separator);
   // 文件路径
   String filePath = webUploadPath.concat(datdDirectory);
 
   File dest = new File(filePath, newFileName);
   if (!dest.getParentFile().exists()) {
   dest.getParentFile().mkdirs();
   }
   // 判断是否有旧头像,如果有就先删除旧头像,再上传
   SUser userInfo = sUserService.findUserInfo(userId.toString());
   if (StringUtils.isNotBlank(userInfo.getUserHead())) {
   String oldFilePath = webUploadPath.concat(userInfo.getUserHead());
   File oldFile = new File(oldFilePath);
   if (oldFile.exists()) {
   oldFile.delete();
   }
   }
   // 上传到指定目录
   file.transferTo(dest);
 
   // 将图片流转换进行BASE64加码
   //BASE64Encoder encoder = new BASE64Encoder();
   //String data = encoder.encode(file.getBytes());
 
   // 将反斜杠转换为正斜杠
   String data = datdDirectory.replaceAll( "\\\\" , "/" ) + newFileName;
   Map<String, Object> resultMap = new HashMap<>();
   resultMap.put( "file" , data);
   resultVo.setData(resultMap);
   resultVo.setError( 1 , "上传成功!" );
  } catch (IOException e) {
   resultVo.setError( 0 , "上传失败!" );
  }
  } else {
  resultVo.setError( 0 , "上传的文件不是图片类型,请重新上传!" );
  }
  return resultVo;
  } else {
  resultVo.setError( 0 , "上传失败,请选择要上传的图片!" );
  return resultVo;
  }
  }
 
}

以上代码需配置SUserService,一个业务层接口; 。

一个ResultVo对象,属性已给出; 。

一个基于Spring Boot的 .yml配置文件的配置。  。

访问图片的时候,需要配置.yml文件 。

spring

#配置http访问服务器图片的路径 resources: static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path} 。

然后基于服务的IP与端口,http//IP:port/resources/static/图片路径(图片名) 。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

最后此篇关于Spring Boot实现图片上传功能的文章就讲到这里了,如果你想了解更多关于Spring Boot实现图片上传功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com