Feign与Dubbo文件上传

Feign与Dubbo文件上传

1.Feign

生产者

feign

/**
     * 规则协议图片上传
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    RestResponse<OssUploadDTO> upload(@RequestPart(value = "file",required = true) MultipartFile file);

controller

/**
     * 规则协议图片上传
     *
     * @param file
     * @return
     */
    @Override
    @PostMapping("/upload")
    public RestResponse<OssUploadDTO> upload(@RequestParam(value = "file",required = true) MultipartFile file) {
        try {
            OssUploadDTO dto = OssUploadUtils.uploadAndGenerateTmpUrl(file,"customer/images");
            return RestResponse.success(dto);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return RestResponse.fail(e.getMessage());
        }
    }

uploadAndGenerateTmpUrl

/**
 * 图片上传
 *
 * @author linfan.sun
 * @description:
 * @time: 2021/1/18 15:40
 */
@Slf4j
public class OssUploadUtils {

    public static OssUploadDTO uploadAndGenerateTmpUrl(MultipartFile file, String fileDir) {
        log.info("图片上传file.getResource():{}",file.getResource());
        String fileName = null;
        fileName = file.getResource().getFilename();
        log.info("图片上传fileName:{}",fileName);
        assert fileName != null;
        String name = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
        log.info("name:{}",name);
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        AliOSSCloudUtil util = new AliOSSCloudUtil();
        //上传成功返回完整路径
        OssUploadDTO dto = util.uploadFile2OSSResult(inputStream, name, fileDir + "/", "dyyy-static");
        util.close();
        return dto;
    }
}

消费者

 /**
     * 规则协议图片上传
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    RestResponse<OssUploadDTO> upload(@RequestParam(value = "file") MultipartFile file){
        return sfcAgreementClient.upload(file);
    }

2.Dubbo

生产者
dubbo

    /**
     * 规则协议图片上传
     *
     * @param fileBytes
     * @return
     */
    OssUploadDTO uploadFile(byte[] fileBytes,String name) throws IOException;

dubbo实现

    @Override
    public OssUploadDTO uploadFile(byte[] fileBytes,String name) throws IOException {
        InputStream inputStream = new ByteArrayInputStream(fileBytes);
        MultipartFile file = new MockMultipartFile(name,null,ContentType.MULTIPART_FORM_DATA.toString(), inputStream);
        OssUploadDTO dto = OssUploadFileUtils.uploadAndGenerateTmpUrl(file, "App-server/images/customer-service",name);
        return dto;
    }

uploadAndGenerateTmpUrl

    public static OssUploadDTO uploadAndGenerateTmpUrl(MultipartFile file, String fileDir,String fileName) {
        log.info("图片上传file.getResource():{}",file.getResource());
        String name = UUID.randomUUID().toString().replace("-", "") + fileName.substring(fileName.lastIndexOf("."));
        log.info("name:{}",name);
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
        AliOSSCloudUtil util = new AliOSSCloudUtil();
        //上传成功返回完整路径
        OssUploadDTO dto = util.uploadFile2OSSResult(inputStream, name, fileDir + "/", "dyyy-static");
        util.close();
        return dto;
    }

controller

    @PostMapping("/uploadFile")
    public RestResponse<OssUploadDTO> uploadFile(byte[] bytes,String fileName) {
        try {
            OssUploadDTO dto = agreementService.uploadFile(bytes,fileName);
            return RestResponse.success(dto);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return RestResponse.fail(e.getMessage());
        }
    }

消费者




    @DubboReference(version = "1.0.0")
    private AgreementService agreementService;
 /**
     * 协议图片上传
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public RestResponse<OssUploadDTO> uploadFile(@RequestPart(value = "file") MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            String fileName = file.getResource().getFilename();
            OssUploadDTO dto = agreementService.uploadFile(bytes,fileName);
            return RestResponse.success(dto);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return RestResponse.fail(e.getMessage());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值