package com.itmuch.cloud.study;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic 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;@Controllerpublic 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#加入到eurekaeureka: client: serviceUrl: defaultZone: http://user:password123@localhost:8761/eureka/ instance: prefer-ip-address: truespring: application: name: microservice-file-upload http: multipart: max-file-size: 2000Mb # Max file size,默认1M max-request-size: 2500Mb # Max request size,默认10M
4.0.0 com.itmuch.cloud microservice-file-upload 0.0.1-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 1.4.2.RELEASE UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-eureka org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-dependencies Camden.SR2 pom import org.springframework.boot spring-boot-maven-plugin