Feign中不支持文件上传,需要引用外部依赖进行相应的配置才能实现。
1、Eureka注册中心
1.1、引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent>
<properties> <spring.cloud.version>Camden.SR4</spring.cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
</dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
|
1.2、配置文件application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server: port: 9000
spring: application: name: springcloud-eureka
eureka: server: enable-self-preservation: false client: healthcheck: enable: true register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://127.0.0.1:9000/eureka/
|
1.3、注册中心启动类
1 2 3 4 5 6 7 8 9 10 11 12
| import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication @EnableEurekaServer public class EurekaApplication {
public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
|
2、文件上传服务
2.1、文件上传类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.IOException;
@RestController public class FileUploadController {
@RequestMapping(value = "upload", method = RequestMethod.POST) public String fileUplaod(@RequestPart(value = "file") MultipartFile file) throws IOException { byte[] bytes = file.getBytes(); File file2save = new File(file.getOriginalFilename()); FileCopyUtils.copy(bytes, file2save); return file2save.getAbsolutePath(); } }
|
2.2、配置文件application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server: port: 8007
eureka: client: serviceUrl: defaultZone: http://127.0.0.1:9000/eureka/ instance: prefer-ip-address: true
spring: application: name: springcloud-ms-file-upload http: multipart: max-file-size: 50MB max-request-size: 100MB
|
2.3、上传服务启动类
1 2 3 4 5 6 7 8 9 10 11 12
| import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient @SpringBootApplication public class FileUploadApplication {
public static void main(String[] args) { SpringApplication.run(FileUploadApplication.class, args); } }
|
3、客户端
3.1、引入依赖
需要引入额外的依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
|
完整依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent>
<properties> <spring.cloud.version>Camden.SR4</spring.cloud.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency>
</dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
|
3.2、UploadFeignClient
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import feign.codec.Encoder; import feign.form.spring.SpringFormEncoder; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.multipart.MultipartFile;
@FeignClient(name = "springcloud-ms-file-upload", configuration = UploadFeignClient.MultipartConfig.class) public interface UploadFeignClient {
@RequestMapping(value = "upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String upload(@RequestPart(value = "file") MultipartFile file);
@Configuration class MultipartConfig { @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(); } } }
|
注意:上传的文件使用@RequestPart注解
3.3、配置文件application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 8003
eureka: client: serviceUrl: defaultZone: http://127.0.0.1:9000/eureka/ instance: prefer-ip-address: true
spring: application: name: springcloud-client
|
3.4、客户端上传接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import org.lee.springcloud.client.UploadFeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@RestController public class UploadController {
@Resource private UploadFeignClient uploadFeignClient;
@RequestMapping(value = "upload", method = RequestMethod.POST) public String fileUplaod(@RequestPart(value = "file") MultipartFile file) { return uploadFeignClient.upload(file); } }
|
3.5、客户端启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class ClientApplication {
public static void main(String[] args) { SpringApplication.run(ClientApplication.class, args); } }
|
4、文件上传测试
首先启动Eureka注册中心,再启动文件上传服务,最后启动客服端,
通过Postman进行文件上传测试,访问客户端文件上传接口:http://127.0.0.1:8003/upload。
Body中选择form-data,参数名,file,类型:file,value:选择上传的文件,点击Send即可。