• springcloud14---zuul


    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);
      }
    }
    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
       使用cmd命令:curl -F "file=@文件全名" localhost:8050/upload
      @return 文件在服务器上的绝对路径
       */
        
    //把zuul启动起来(8040端口),通过http://localhost:8040/microservice-file-upload/upload访问
    //也可以通过http://localhost:8040/zuul/microservice-file-upload/upload
    //zuul上传小文件不加/zuul前缀,大文件加/zuul绕过spring的。
    /*
    zuul上传大文件要在zuul工程的配置文件设置超时:
    #经过zuul的请求都会通过hysitrcs包裹,所以zuul会有断路器功能,配置hysitrcs的超时时间
    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
    #zuul还使用了ribbon做负载均衡,要设备ribbon的超时时间
    ribbon:
      ConnectTimeout: 3000
      ReadTimeout: 60000
    */
      @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();
      }
    }
    server:
      port: 8050
    #加入到eureka
    eureka:
      client:
        serviceUrl:
          defaultZone: http://user:password123@localhost:8761/eureka/
      instance:
        prefer-ip-address: true
    spring:
      application:
        name: microservice-file-upload
      http:
        multipart:
          max-file-size: 2000Mb      # Max file size,默认1M
          max-request-size: 2500Mb   # Max request size,默认10M
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.itmuch.cloud</groupId>
      <artifactId>microservice-file-upload</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>
    
      <!-- 引入spring boot的依赖 -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
      </parent>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
      </properties>
    
      <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>
    
      <!-- 引入spring cloud的依赖 -->
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR2</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
    
      <!-- 添加spring-boot的maven插件 -->
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>
  • 相关阅读:
    手把手实战:eclipse 搭建 SpringMvc 框架环境
    解决eclipse中Tomcat服务器的server location选项不能修改的问题
    如何解决JSP页面顶端报错 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    第二课 --- git的(管理修改和撤销修改、删除文件)
    第二课 ---git时光穿梭(版本回退)
    第一课——git的简介和基本使用
    001——使用composer安装ThinkPHP5
    微信小程序中对于变量的定义
    微信小程序onLaunch修改globalData的值
    7——ThinkPhp中的响应和重定向:
  • 原文地址:https://www.cnblogs.com/yaowen/p/9159426.html
Copyright © 2020-2023  润新知