栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot实现图片上传功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot实现图片上传功能

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

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;


@RestController
public class FileUpload {
 
 
 @Autowired
 private SUserService sUserService;
 
 
 @Value("${web.upload-path}")
 private String webUploadPath;
 
 

 
 @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 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上传下载专题》进行深入学习和研究。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/146006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号