springcloud 使用feign 上传图片
Posted 正怒月神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud 使用feign 上传图片相关的知识,希望对你有一定的参考价值。
一,上传图片接口
微服务:web-common
这个接口提供给feign 来RPC调用
@RequestMapping(value = "upload", method = RequestMethod.POST)
public ApiResult<FileVO> upload(HttpServletRequest request) throws Exception {
FileVO fileVO =new FileVO();
MultipartHttpServletRequest murequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> files = murequest.getFileMap();// 得到文件map对象
for (MultipartFile file : files.values()) {
//内部操作逻辑,我上传到MINIO
fileVO = fileService.upload(file);
}
ApiResult<FileVO> result = new ApiResult<>();
result.setCodeToSuccessed();
result.setData(fileVO);
return result;
}
// fileService.upload方法
@Override
public FileVO upload(MultipartFile file) throws Exception {
//源文件名
String originalFilename = file.getOriginalFilename();
//保存文件的路径(如2020/7/10/4d132f66a7b948c882e2c1649a58ab70.jpg)
String saveFileName = FileHelper.getFileName(FileHelper.getFileEx(originalFilename));
//初始化MinioClient
MinioClient minioClient = MinioClient.builder()
.endpoint(minioHostUrl)
.credentials(minioAccessKey, miniosecretKey)
.build();
//上传
minioClient.putObject(
PutObjectArgs.builder()
.bucket(minioBucketName) //桶名(文件夹名)
.stream(file.getInputStream(), file.getSize(), 5 * 1024 * 1024) //文件,大小
.object(saveFileName) //文件名
.build());
String backFileName = "/" + minioBucketName + "/" + saveFileName;
return backFileName;
}
二,fegin调用
微服务:web-test
POM
<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-core -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>11.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
三,编写feign
package com.tenyears.webAD.feign;
import com.tenyears.model.common.ApiResult;
import com.tenyears.model.webCommon.FileVO;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
/**
* @description :
* @auther Tyler
* @date 2021/6/15
*/
@FeignClient(value = "web-common")
public interface WebCommonFeign {
@RequestMapping(value = "api/file/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
ApiResult<FileVO> upload(@RequestPart("file") MultipartFile file);
@Configuration
class FeignConfig {
@Bean
public Encoder multipartFormEncoder() {
return new SpringFormEncoder();
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
}
四,controller
@RestController
@RequestMapping("api/test")
public class TestController {
Logger logger = LoggerFactory.getLogger(TestController.class);
@Autowired
WebCommonFeign webCommonFeign;
/**
* feign 上传图片
* @param file
* @return
*/
@RequestMapping(value = "test4", method = RequestMethod.POST)
public ApiResult<FileVO> test4(MultipartFile file) {
ApiResult<FileVO> result = webCommonFeign.upload(file);
return result;
}
}
五,效果
以上是关于springcloud 使用feign 上传图片的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法
Spring Cloud下使用Feign Form实现微服务之间的文件上传
SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法
Java网络商城项目 SpringBoot+SpringCloud+Vue 网络商城(SSM前后端分离项目)八(文件的上传FastDFS和校验)(Nginx的请求前缀配置,在发布项目的时候要注意)(代