使用Zuul上传文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/80805667
一 介绍
对于小文件(1M以内上传),无须任何处理,即可正常上传。对于大文件(10M以上)上传,需要为上传路径添加/zuul前缀。也可使用zuul.servlet-path自定义前缀。
假设zuul.routes.microservice-file-upload=/microservice-file-upload/**
如果http://{HOST}:{PORT}/upload是微服务microservice-file-upload的上传路径,则可使用Zuul的/zuul/microservice-file-upload/upload路径上传大文件。
如果Zuul使用了Ribbon做负载均衡,那么对于超大的文件(例如500M),需要提高超时设置,例如:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
二 实例
1 创建Maven工程,创建项目microservice-file-upload,并添加以下依赖:
 <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
2 在启动类上添加@SpringBootApplication和@EnableEurekaClient
package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class FileUploadApplication {
  public static void main(String[] args) {
    SpringApplication.run(FileUploadApplication.class, args);
  }
}
3 编写Controller
package com.itmuch.cloud.study.controller;

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

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

@Controller
public class FileUploadController {
  /**
   * 上传文件
   * 测试方法:
   * 有界面的测试:http://localhost:8050/index.html
   * 使用命令:curl -F "file=@文件全名" localhost:8050/upload
   * ps.该示例比较简单,没有做IO异常、文件大小、文件非空等处理
   * @param file 待上传的文件
   * @return 文件在服务器上的绝对路径
   * @throws IOException IO异常
   */
  @RequestMapping(value = "/upload", method = RequestMethod.POST)
  public @ResponseBody String handleFileUpload(@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();
  }
}
4 配置application.yml
server:
  port: 8050
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
spring:
  application:
    name: microservice-file-upload
  http:
    multipart:
      max-file-size: 500Mb      # Max file size,默认1M
      max-request-size: 1000Mb   # Max request size,默认10M
三 测试
1 启动eureka
2 启动microservice-file-upload
3 启动microservice-gateway-zuul
4 用postman的 http://localhost:8050/upload上传小文件和大文件,都能上传成功
5 测试Zuul上传小文件,上传成功,用postman测试: http://localhost:8040/microservice-file-upload/upload
6 测试Zuul上传大文件,上传失败,用postman测试:http://localhost:8040/microservice-file-upload/upload
7 测试Zuul上传大文件,上传失败,用postman测试: http://localhost:8040/zuul/microservice-file-upload/upload
8 修正一下zuul的配置如下
server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone:http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
# 上传大文件得将超时时间设置长一些,否则会报超时异常。以下几行超时设置来自http://cloud.spring.io/spring-cloud-static/Camden.SR3/#_uploading_files_through_zuul
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
测试Zuul上传大文件,上传成功,用postman测试: http://localhost:8040/zuul/microservice-file-upload/upload
四 参考
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页