一,上傳圖片接口
微服務: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 MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
注意上面的@Configuration
這樣配置又能上傳文件,又能傳遞RequestBody!
@Configuration class MultipartSupportConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } }
四,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; }
