欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springcloud如何使用Feign后臺內(nèi)部傳遞MultipartFile

 更新時(shí)間:2022年03月04日 11:52:51   作者:linli1991  
這篇文章主要介紹了springcloud如何使用Feign后臺內(nèi)部傳遞MultipartFile,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

如何使用Feign后臺內(nèi)部傳遞MultipartFile

先修改Feign Client接口

 
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
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;
 
/**
 * @author linli
 * @date 20-06-27
 */
@FeignClient(value = "upload", fallbackFactory = UploadFallbackFactory.class, configuration = UploadClient.MultipartSupportConfig.class)
public interface UploadClient {
 
    @PostMapping(path = "/upload-text", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String uploadText(@RequestPart(name = "file") MultipartFile file);
 
    /**
     * 引用配置類MultipartSupportConfig.并且實(shí)例化
     */
    class MultipartSupportConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}

若SpringFormEncoder 引入報(bào)錯(cuò),加上下面的依賴

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
	<version>3.3.0</version>
</dependency>
<dependency>
	<groupId>io.github.openfeign.form</groupId>
	<artifactId>feign-form-spring</artifactId>
	<version>3.3.0</version>
</dependency>

內(nèi)部調(diào)用

private String uploadFile(String str) {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        MultipartFile multipartFile = null;
        byte[] bt = str.getBytes();
        File file = null;
        try {
            file = File.createTempFile("file" + UUID.randomUUID(), ".txt");
            fos = new FileOutputStream(file);
            fos.write(bt, 0, bt.length);
            fis = new FileInputStream(file);
            multipartFile = new MockMultipartFile("file", file.getName(),
                    MediaType.TEXT_PLAIN_VALUE, fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return uploadClient.uploadText(multipartFile);
    }

注意點(diǎn)

Feign進(jìn)行跨服務(wù)傳遞MultipartFile文件

通過調(diào)用服務(wù)進(jìn)行文件上傳,避免每個(gè)需要上傳文件的模塊都寫一遍上傳服務(wù),造成代碼冗余。

本文主要包含通過feign進(jìn)行文件上傳模塊。

使技術(shù)人員在開發(fā)過程中遇到問題時(shí)有地可查,有章可循。

通過feign進(jìn)行跨服務(wù)傳遞MultipartFile文件

添加依賴

<dependency>
? ?<groupId>io.github.openfeign.form</groupId>
? ?<artifactId>feign-form</artifactId>
? ?<version>3.0.3</version>
</dependency>
<dependency>
? ?<groupId>io.github.openfeign.form</groupId>
? ?<artifactId>feign-form-spring</artifactId>
? ?<version>3.0.3</version>
</dependency>

添加配置文件

package com.ruiyi.twowayreferral.configurer;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
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;
@Configuration
public class MultipartSupportConfig {
? ?@Autowired
? ?private ObjectFactory<HttpMessageConverters> messageConverters;
? ?@Bean
? ?public Encoder feignFormEncoder() {
? ? ? ?return new SpringFormEncoder(new SpringEncoder(messageConverters));
? ?}
}

代碼示例

@FeignClient(value = "controller-center")
public interface CallFrignService {
? ? /**
? ? ?* @Create 文件上傳 wanggx_ruiyi 2019.11.15
? ? ?* @param uploadPath 文件上傳地址
? ? ?* @param file 上傳的文件
? ? ?* @return
? ? ?*/
? ?@PostMapping(value = "/api/v1/common/file/fileUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
? ?String fileUpload(@RequestParam(value = "uploadPath", required = true) String uploadPath,@RequestPart(value = "file", required = true) MultipartFile file);
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 只需兩步實(shí)現(xiàn)Eclipse+Maven快速構(gòu)建第一個(gè)Spring Boot項(xiàng)目

    只需兩步實(shí)現(xiàn)Eclipse+Maven快速構(gòu)建第一個(gè)Spring Boot項(xiàng)目

    這篇文章主要介紹了只需兩步實(shí)現(xiàn)Eclipse+Maven快速構(gòu)建第一個(gè)Spring Boot項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • Java?Spring?循環(huán)依賴解析

    Java?Spring?循環(huán)依賴解析

    這篇文章主要介紹了Java?Spring?循環(huán)依賴解析,Spring?現(xiàn)在其實(shí)是我們?Java?程序開發(fā)離不開的基礎(chǔ)框架,個(gè)人覺得除了?JDK?我們用得最多的?Java?中間件就是?Spring?,今天我們一起來學(xué)習(xí)一下?Spring?的循環(huán)依賴。下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-02-02
  • SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié)

    SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié)

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring?框架的?MethodInterceptor?簡介及示例代碼

    Spring?框架的?MethodInterceptor?簡介及示例代碼

    MethodInterceptor接口定義了一個(gè)方法Object?intercept(Object?obj,?Method?method,?Object[]?args,?MethodProxy?proxy)?,該方法在代理對象的方法被調(diào)用時(shí)被觸發(fā),這篇文章主要介紹了Spring?框架的?MethodInterceptor?簡介及示例代碼,需要的朋友可以參考下
    2023-09-09
  • Java中sharding-jdbc按年月分片的示例代碼

    Java中sharding-jdbc按年月分片的示例代碼

    本文主要介紹了Java中sharding-jdbc按年月分片的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理

    如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理

    這篇文章主要介紹了如何利用 Either 和 Option 進(jìn)行函數(shù)式錯(cuò)誤處理。在 Java 中,錯(cuò)誤的處理在傳統(tǒng)上由異常以及創(chuàng)建和傳播異常的語言支持進(jìn)行。但是,如果不存在結(jié)構(gòu)化異常處理又如何呢?,需要的朋友可以參考下
    2019-06-06
  • SpringBoot整合Mybatis實(shí)現(xiàn)商品評分的項(xiàng)目實(shí)踐

    SpringBoot整合Mybatis實(shí)現(xiàn)商品評分的項(xiàng)目實(shí)踐

    本文介紹了SpringBoot整合Mybatis-plus框架實(shí)現(xiàn)對商品評分的功能實(shí)現(xiàn)流程和前端接口實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-02-02
  • Spring?Validation接口入?yún)⑿r?yàn)示例代碼

    Spring?Validation接口入?yún)⑿r?yàn)示例代碼

    Spring?Validation是一種用于實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的框架,它提供了一系列的校驗(yàn)器,針對不同的數(shù)據(jù)類型可以使用不同的校驗(yàn)器進(jìn)行校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于Spring?Validation接口入?yún)⑿r?yàn)的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • JAVA中DIFF算法實(shí)現(xiàn)

    JAVA中DIFF算法實(shí)現(xiàn)

    本文主要介紹了JAVA中DIFF算法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java自動拆箱空指針異常的解決

    Java自動拆箱空指針異常的解決

    這篇文章主要介紹了Java自動拆箱空指針異常的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論