SpringCloud微服务之间传输文件

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();
        }
    }
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风水道人

写作不易且行且珍惜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值