1、在nacos中配置minio参数
2、controller层
package com.heima.wemedia.controller.v1;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {
@Autowired
WmMaterialService wmMaterialService;
@PostMapping("/upload_picture")
public ResponseResult upload(MultipartFile multipartFile){
return wmMaterialService.upload(multipartFile);
}
}
3、接口实现类
封装minio上传工具Minio: 封装MinIO为starter_moreCalm的博客-CSDN博客文章来源:https://www.toymoban.com/news/detail-634091.html
获取用户id的前置工作 微服务:从header中获取用户存入当前线程_moreCalm的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-634091.html
package com.heima.wemedia.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.wemedia.model.po.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;
/**
* <p>
* 自媒体图文素材信息表 服务实现类
* </p>
*
* @author itcast
* @since 2023-08-05
*/
@Service
@Slf4j
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
@Autowired
private FileStorageService fileStorageService;
/*图片上传*/
@Override
public ResponseResult upload(MultipartFile file) {
/*参数校验*/
if(file == null || file.getSize() <= 0){
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
/*上传图片到minio*/
//生成uuid
String fileName = UUID.randomUUID().toString().replace("-", "");
//获取图片名称
String originalFilename = file.getOriginalFilename();
//获取文件扩展名(后缀名)
String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
//定义文件id用来接收上传的图片
String fileId = null;
try {
fileId = fileStorageService.uploadImgFile("", fileName + postfix, file.getInputStream());
log.info("上传图片到MinIO中,fileId:{}",fileId);
} catch (IOException e) {
e.printStackTrace();
log.error("WmMaterialServiceImpl-上传文件失败");
}
/*保存数据到数据库中*/
WmMaterial wmMaterial = new WmMaterial();
wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());//用户id
wmMaterial.setUrl(fileId);//图片地址
wmMaterial.setType(0);//文件类型(0 图片 1 视频)
wmMaterial.setCollection(0);//是否收藏
wmMaterial.setCreatedTime(LocalDateTime.now());//创建时间
save(wmMaterial);//保存数据库
/*返回结果*/
return ResponseResult.okResult(wmMaterial);
}
}
到了这里,关于MinIO:微服务中上传图片流程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!