1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
      SpringCloudFeign的文件上傳實(shí)現(xiàn)的示例代碼

      在Spring Cloud封裝的Feign中并不直接支持傳文件,但可以通過(guò)引入Feign的擴(kuò)展包來(lái)實(shí)現(xiàn),本來(lái)就來(lái)具體說(shuō)說(shuō)如何實(shí)現(xiàn)。

      創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供木蘭網(wǎng)站建設(shè)、木蘭做網(wǎng)站、木蘭網(wǎng)站設(shè)計(jì)、木蘭網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、木蘭企業(yè)網(wǎng)站模板建站服務(wù),十多年木蘭做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

      服務(wù)提供方(接收文件)

      服務(wù)提供方的實(shí)現(xiàn)比較簡(jiǎn)單,就按Spring MVC的正常實(shí)現(xiàn)方式即可,比如:

      @RestController
      public class UploadController {
      
        @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
          return file.getName();
        }
        
      }
      

      服務(wù)消費(fèi)方(發(fā)送文件)

      在服務(wù)消費(fèi)方由于會(huì)使用Feign客戶端,所以在這里需要在引入feign對(duì)表單提交的依賴,具體如下:

      
        io.github.openfeign.form
        feign-form
        3.0.3
      
      
        io.github.openfeign.form
        feign-form-spring
        3.0.3
      
      
        commons-fileupload
        commons-fileupload
      
      

      定義FeignClient,假設(shè)服務(wù)提供方的服務(wù)名為 upload-server

      @FeignClient(value = "upload-server", configuration = TestServiceClient.MultipartSupportConfig.class)
      public interface UploadService { 
        @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        String handleFileUpload(@RequestPart(value = "file") MultipartFile file);
       
        @Configuration
        class MultipartSupportConfig {
          @Bean
          public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
          }
        } 
      }

      在啟動(dòng)了服務(wù)提供方之后,嘗試在服務(wù)消費(fèi)方編寫(xiě)測(cè)試用例來(lái)通過(guò)上面定義的Feign客戶端來(lái)傳文件,比如:

      @Test
      @SneakyThrows
      public void testHandleFileUpload() { 
        File file = new File("files/aaa.txt");
        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",
            MediaType.TEXT_PLAIN_VALUE, true, file.getName()); 
        try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
          IOUtils.copy(input, os);
        } catch (Exception e) {
          throw new IllegalArgumentException("Invalid file: " + e, e);
        } 
        MultipartFile multi = new CommonsMultipartFile(fileItem); 
        log.info(testServiceClient.handleFileUpload(multi));
      }
      

      以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


      網(wǎng)頁(yè)名稱:SpringCloudFeign的文件上傳實(shí)現(xiàn)的示例代碼
      轉(zhuǎn)載源于:http://www.ef60e0e.cn/article/iecpph.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        根河市| 磐安县| 左权县| 蓬安县| 安泽县| 旺苍县| 裕民县| 资源县| 安平县| 琼中| 衡水市| 石河子市| 新邵县| 南开区| 巩留县| 肇源县| 盐边县| 苏尼特右旗| 秦皇岛市| 仁化县| 闻喜县| 铁力市| 隆林| 巨野县| 乌兰县| 罗山县| 浦县| 延吉市| 鹿邑县| 泰和县| 门源| 资中县| 湟中县| 马尔康县| 历史| 延津县| 左云县| 崇州市| 桓仁| 沙河市| 重庆市|