zuul上传文件

1、文件上传服务的pom.xml

   
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
         
    
    com.strive
    eureka-client-fileupload
    0.0.1-SNAPSHOT
    eureka-client-fileupload
    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
        Finchley.SR1
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            
            
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

2、application.yml

server:
  port: 8604
spring:
  application:
    name: eureka-client-fileupload
  servlet:    #注意这块是servlet,之前的http已经被弃用,设置http相当于没有设置
    multipart:
      max-file-size: 500MB
      max-request-size: 1000MB
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
#    healthcheck:
#      enabled: true
  instance:
    prefer-ip-address: true

 3、启动类

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientFileuploadApplication {

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

}

4、 上传代码

@RestController
public class HomePageController {
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file){
        if(file.isEmpty()){
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" + size);

        String path = "E:/test" ;
        File dest = new File(path + "/" + fileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            return dest.getAbsolutePath();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }
}

5、zuul的代码和上篇zuul的最基本的配置相同

6、请求示例:

(1)上传小文件zuul上传文件_第1张图片

(2)上传大文件(报错)

zuul上传文件_第2张图片

更改请求,大文件请求在/zuul/**的后面,用以绕过Spring DispatcherServlet的处理(用以避免多次处理)

zuul上传文件_第3张图片

 结果报错TimeOut,这是因为文件过大,请求事件过长,Hystrix被调用返回了,所以我们在zuul中配置

spring:
  application:
    name: eureka-client-zuul
server:
  port: 8601
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    healthcheck:
      enabled: true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
logging:
  level:
    com.netflix: debug

再次请求:

zuul上传文件_第4张图片

文件上传成功 

你可能感兴趣的:(SpringCloud)