Zuul【文件上傳】


1、搭建一個eureka-server注冊中心工程

該工程比較簡潔,沒有太多配置,不在描述,單節點,服務端口:8888

2、創建zuul-gateway網關工程

2.1、工程pom依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
</dependencies>

 

2.2、工程配置文件:zuul-gateway\src\main\resources\bootstrap.yml

spring:
  application:
    name: zuul-gateway
  servlet:  #spring boot2.0之前是http
    multipart:
      enabled: true   # 使用http multipart上傳處理
      max-file-size: 100MB # 設置單個文件的最大長度,默認1M,如不限制配置為-1
      max-request-size: 100MB # 設置最大的請求文件的大小,默認10M,如不限制配置為-1
      file-size-threshold: 10MB  # 當上傳文件達到10MB的時候進行磁盤寫入
      location: /  # 上傳的臨時目錄
server:
  port: 5555
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8888}/eureka/
  instance:
    prefer-ip-address: true
    
##### Hystrix默認超時時間為1秒,如果要上傳大文件,為避免超時,稍微設大一點
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 30000

注意:

SpringBoot版本之間有很大的變化,1.4.x 版本前:

multipart: enabled: true max-file-size: 100MB 

1.5.x - 2.x 之間:

spring: http: multipart: enabled: true max-file-size: 100MB 

2.x 之后:

spring: servlet: multipart: enabled: true max-file-size: 100MB 

 

2.3、網關工程啟動類:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class, args);
    }
}

 

2.4、上傳代碼:

import java.io.File;
import java.io.IOException;

import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class ZuulUploadController {

    @PostMapping("/upload")
    @ResponseBody
    public String uploadFile(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        File fileToSave = new File(file.getOriginalFilename());
        FileCopyUtils.copy(bytes, fileToSave);
        return fileToSave.getAbsolutePath();
    }
}  

 

3、測試,上傳成功返回文件絕對路徑:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM