SpringCloud微服务之间传输文件:
1.引入jar
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>2.1.0</version>
</dependency>
2.添加配置类
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
@Configuration
public class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
3.调用其他微服务上传文件的FeignClient
import com.yl.soft.princess.common.config.MultipartSupportConfig;
import com.yl.soft.princess.dto.base.ResultItem;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
@FeignClient(value = "live", contextId = "uploadVideoService",configuration = MultipartSupportConfig.class, fallbackFactory = LiveServiceFallbackFactory.class)
public interface UploadVideoService {
//注意注解名称,consumes ,RequestPart
@PostMapping(value = "/signature/uploadVideo",consumes = MediaType.MULTIPART_FORM_DATA_VALUE )
ResultItem uploadVideo(@RequestPart("file") MultipartFile file);
}
4.前端要调用上传文件的Controller
import com.alibaba.fastjson.JSON;
import com.yl.soft.princess.common.util.StringUtilsEX;
import com.yl.soft.princess.controller.BaseController;
import com.yl.soft.princess.dto.base.ResultItem;
import com.yl.soft.princess.ek.course.CourseService;
import com.yl.soft.princess.ek.live.LiveService;
import com.yl.soft.princess.ek.live.UploadVideoService;
import com.yl.soft.princess.ek.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@RestController
@RequestMapping("/signature")
public class PlatformVideoController extends BaseController {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
// @Autowired
// private LiveService liveService;
@Resource
private UploadVideoService uploadVideoService;
/**
* 上传视频
*
* @param id 小视频id
* @param sort 排序
*/
@PostMapping("/uploadVideo")
public ResultItem videoSort(@RequestParam("file") MultipartFile file) {
try {
ResultItem resultItem = uploadVideoService.uploadVideo(file);
System.out.println(resultItem);
return resultItem;
} catch (Exception e) {
e.printStackTrace();
logger.error("修改小视频排序出错! 异常信息:{}", e.getMessage());
return error();
}
}
}