在SpringCloud中文件上传可以采用FeignForm或者直接HttpClient方式进行处理,如下所示
一、用FeignClient进行文件上传
1.1、需要增加对应的pom文件
io.github.openfeign.form
feign-form
io.github.openfeign.form
feign-form-spring
1.2、定义服务端的上传接口跟普通springMVC文件上传接口类同:
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Result uploadFile(
@RequestPart("file") MultipartFile file,
@RequestParam("param1") String param1,
@RequestParam(value = "param2",required = false) String param2) {
需要指定对应的consumes为MediaType.MULTIPART_FORM_DATA_VALUE,如果是多个参数,则需要指定file参数为@RequestPart("file") MultipartFile file
1.3 增加供客户端调用的Feign接口
@FeignClient(value = "${service_name}",configuration = UploadClientConfiguration.class ,contextId = "${service_name}ClientFileApi")
public interface ClientFileApi {
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Result uploadFile(
@RequestPart("file") MultipartFile file,
@RequestParam("param1") String param1,
@RequestParam(value = "param2",required = false) String param2) {
public class UploadClientConfiguration {
/**
* 此处注入的是: ObjectFactory
*/
@Autowired
private ObjectFactory messageConverters;
@Bean
public Encoder feignEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
需要注意的是FeignClient接口中的configuration必须为FeignForm对应的配置UploadClientConfiguration
1.4 客户端调用,直接接收到文件后,进行转发即可,如下所示:
@Resource
ClientFileApi clientFileApi;
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity uploadFile(
@RequestParam("file") MultipartFile mpfile){DiskFileItem fileItem =(DiskFileItem) new DiskFileItemFactory().createItem("featureFile",MediaType.ALL.toString(),true, featureFile.getOriginalFilename());
try (InputStream input = featureFile.getInputStream(); OutputStream os = fileItem.getOutputStream()) {
IOUtils.copy(input, os);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid file: " + e, e);
}
MultipartFile file = new CommonsMultipartFile(fileItem);
clientFileApi.upload(file,"","");
}
在使用FeignClient进行文件上传时,报如下的错误:
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
需要服务器端配置@EnabledMVc,并且配置上传文件相关属性
如下所示:
/***
*
*/
@Configuration
@EnableWebMvc
public class WebMvcConfiguration implements WebMvcConfigurer {
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxInMemorySize(10240);
multipartResolver.setMaxUploadSizePerFile(10*1024*1024);
multipartResolver.setResolveLazily(false);
return multipartResolver;
}
}
二、直接用RestTemplate进行上传即可
/***
* 进行文件上传处理
* @param file
* @param param1
* @param param2
* @return
*/
public boolean uploadFile(MultipartFile file, String param1, String param2)
{
String url="http://"+servicename"/file/upload";
try {
//return restTemplate.
MultiValueMap param = new LinkedMultiValueMap<>();
param.add("param1", param1);
param.add("param2", param2);
param.add("files",file.getResource());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity> httpEntity = new HttpEntity<>(param, headers);
ResponseEntity re = restTemplate.postForEntity(url,httpEntity,String.class);