• 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-gatewaysrcmain esourcesootstrap.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、测试,上传成功返回文件绝对路径:

     

  • 相关阅读:
    695. 岛屿的最大面积(深搜)
    147. 对链表进行插入排序(排序)
    566. 重塑矩阵(模拟)
    238. 除自身以外数组的乘积(前后缀积)
    29.Java基础_接口
    C++ STL queue
    C++ STL stack
    C++ STL string
    C面向接口编程和C++多态案例
    单例模式入门
  • 原文地址:https://www.cnblogs.com/idoljames/p/11745763.html
Copyright © 2020-2023  润新知