qq_25472747
西门刮风
2018-11-17 03:03

feign MultipartFile fastdfs跨服务调用文件上传

80
  • spring
  • multipartfile
  • feign

基本情况是:
不是通过浏览器请求,是微服务中在服务启动后自动把制定目录下的文件,通过feign调用文件上传接口,将文件保存到服务器。
参考了各位的建议之后,还是报相同的错误。
贴出一段idea中的异常信息:
feign.FeignException: status 500 reading RemoteFdfsService#fdfsUpload(MultipartFile); content:
{"msg":"Current request is not a multipart request","code":1,"data":null}
at feign.FeignException.errorStatus(FeignException.java:60)
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:89)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:143)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:77)
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:107)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302)
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)

调用部分代码(接口需要的是MultipartFile的文件参数)
String filePath = "F:\mac\pic\20181113184344_type[0]_ItsPlate.jpg";
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile(file.getName(), fileInputStream);
String url = remoteFdfsService.fdfsUpload(multipartFile);

feign:
@FeignClient(value = ServiceNameConstant.FDFS_SERVICE,
configuration = RemoteFdfsService.MultipartSupportConfig.class,
fallbackFactory = RemoteFdfsServiceFallbackFactory.class)
public interface RemoteFdfsService {

/**
 * 上传图片并且生成缩略图
 * @param file
 * @return
 */
@PostMapping(value = "/fdfs/upload_image", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String fdfsUploadImage(@RequestPart(value = "file") MultipartFile file);



/**
 * 引用配置类MultipartSupportConfig.并且实例化
 */
@Configuration
class MultipartSupportConfig {
    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder();
    }
}

}
烦请各路上仙指点一二,多谢。

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答