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>