当前位置: 移动技术网 > IT编程>开发语言>Java > Zuul【文件上传】

Zuul【文件上传】

2019年10月27日  | 移动技术网IT编程  | 我要评论

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

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网