Feign調用文件上傳服務接口樣例

  前面做了Feign調用文件下載服務接口的例子,這裏順帶把Feign調用文件上傳服務接口的例子也一起做了!一樣直接上代碼:

  首先是文件上傳服務提供者的主要代碼

    @PostMapping(value = "/upload")
    public String uploadFile(@RequestPart MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        String fileName = file.getOriginalFilename();
        String storePath = "C:\\java\\upload";//換成你自己的文件上傳存儲目錄
        File destFile = new File(storePath + "\\" + fileName);
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();//注意,是mkdirs不是mkdir,後者只創建文件的直接父目錄,創建不了多層目錄
        }
        try {
            file.transferTo(destFile);
            return destFile.getAbsolutePath();//返回文件上傳成功後的文件存儲完整路徑
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

  接着我們在Postman中先測試一下上述接口是否可用(Postman中使用POST請求,在Body中選擇form-data格式,將Key的參數類型切換成File,在Value中選擇目標文件):

  可以看到上傳成功了,接着我又試着上傳一個大一點的文件,上傳了一個電影,結果翻車了:

  查看IDEA控制檯的報錯輸出看到以下信息:

2020-07-15 17:49:54.227 ERROR 5936 --- [nio-8001-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (1538829581) exceeds the configured maximum (10485760)] with root cause

org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException: the request was rejected because its size (1538829581) exceeds the configured maximum (10485760)

  看到重要信息——SizeLimitExceededException異常,超出大小限制異常!好吧,查看Spring Boot源碼發現文件下載默認單文件最大是1MB,單次多個文件總大小最大是10MB,這也太小了!

  在application.yml中修改一下配置,加大電流!將單文件下載的最大尺寸設爲3GB,單次多個文件總大小最大尺寸設爲10GB:

spring:       
  servlet:
    multipart:
      max-file-size: 3GB
      max-request-size: 10GB

  重新啓動服務提供者,再次測試上傳剛纔1.43GB的電影:

  現在服務提供者的文件上傳服務接口就已經準備好了!

 

  接下來是服務消費者(Feign客戶端)的代碼

  新版本的Feign聲明文件上傳接口也已經很簡單了,下面是我的Feign依賴信息:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>

 

  Feign接口聲明:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

@FeignClient(name = "provider-user")
@RequestMapping(value = "/user")
public interface UserFeignClient extends FeignClientParent {
    @PostMapping(value = "/upload",produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String uploadFile(@RequestPart MultipartFile file);
}

 

  然後是服務消費者的Controller接口:

    @PostMapping(value = "/upload", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String uploadFile(@RequestPart MultipartFile file) {
        return this.userFeignClient.uploadFile(file);
    }

  這裏特別提醒一下:首先一定要注意上述代碼中標紅標粗的producesconsumes@RequestPart註解,特是@RequestPart註解不要寫成了@RequestParam,否則你會翻車的

  另外,Feign調用上傳文件服務接口是藉助了feign-formfeign-form-spring組件,早期的Feign包中不包含這兩個組件,需有額外引用,但現在較新版本的Feign內部集成了這兩個依賴,不用再額外引用了

 

  接着我就先後跑起了Eureka服務端文件上傳服務提供者端→文件上傳服務消費者端,然後輕輕鬆鬆的打開Postman進行了測試:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章