一,上传图片接口
微服务: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; } }
五,效果
顺带一提,图片+参数的传递方式:
被调用方:
@RequestMapping(value = "uploadPic2MovWithSecond", method = RequestMethod.POST) public ApiResult<FileVO> uploadPic2MovWithSecond( @RequestPart("file") MultipartFile file ,@RequestParam("second")int second) throws Exception { }
feign:
@RequestMapping(value = "api/file/uploadPic2MovWithSecond",consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST) ApiResult<FileVO> uploadPic2MovWithSecond( @RequestPart("file") MultipartFile files ,@RequestParam("second")int second) ;
调用方:
@RequestMapping(value = "test6", method = RequestMethod.POST) public ApiResult<FileVO> test6( @RequestPart("file") MultipartFile file ,@RequestParam("second")int second) { ApiResult<FileVO> result = webCommonFeign.uploadPic2MovWithSecond(file,second); return result; }