springboot +springCloud文件上传(Eureka+Feign)
文件上传
1.注册中心
1.pom.xml
eureka的jar环境
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
2.eureka 启动类
eureka服务端注解
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@Slf4j
@SpringBootApplication
@EnableEurekaServer
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
log.info("注册中心启动成功");
}
}
3.application.yml
eureka配置
server:
port: 8000
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
2.服务提供者
1.pom.xml
eureka客户端jar环境
feign环境
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
2.启动类
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@Slf4j
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class UploadFileFileApplication {
public static void main(String[] args) {
SpringApplication.run(UploadFileFileApplication.class,args);
log.info("文件上传服务启动成功");
}
}
3.controller
@Controller
@RequestMapping("/upload")
public class UploadFileController {
@ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public UploadFile upload(@RequestPart("file") MultipartFile file, @RequestParam("filePath") String filePath) {
try {
if (file.isEmpty()) {
return null;
}
UploadFile uploadFile = new UploadFile();
DateFormat df = DateFormat.getDateTimeInstance();
long timeMillis = System.currentTimeMillis();
//上传时间
String time = df.format(new Date(timeMillis));
uploadFile.setUpdateTime(time);
//生产文件名
String name = file.getOriginalFilename();
String fileName = this.stringSplit(name, timeMillis);
uploadFile.setFileName(fileName);
//String filePath = "D:/upload/";
String path = filePath + fileName;
uploadFile.setFileUrl(path);
File dest = new File(path);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹
}
file.transferTo(dest);// 文件写入
return uploadFile;
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
4.返回类型(自定义的)
UploadFile
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class UploadFile {
private String fileName;//文件名
private String updateTime;//上传时间
private String fileUrl;//文件存放路径
}
5.application.yml
server:
port: 9010
spring:
application:
name: uploadFile-Service
servlet:
multipart:
enabled: true
file-size-threshold: 0
max-file-size: 10MB
max-request-size: 10MB
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
3.消费者用feign去调用
1.pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</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>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
<version>9.5.1</version>
<scope>compile</scope>
</dependency>
2.启动类
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ImportResource;
@Slf4j
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class,args);
log.info("启动成功");
}
}
3.feign接口
需要配置类
import cn.ljguo.service.facility.feign.comfig.FeignMultipartSupportConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
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.RequestPart;
import org.springframework.web.multipart.MultipartFile;
@Component
@FeignClient(value = "uploadFile-Service",fallbackFactory = FeignMultipartSupportConfig.class)
public interface SchedualUploadFile {
@RequestMapping(value = "/upload/upload",method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public UploadFile upload(@RequestPart("file") MultipartFile file,@RequestParam("filePath") String filePath);
}
4.FeignMultipartSupportConfig 配置类
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
@Configuration
public class FeignMultipartSupportConfig {
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder();
}
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
5.controller
import lombok.extern.slf4j.Slf4j;
import model.facilityManager.FacilityCode;
import model.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RestController
@RequestMapping("/bag")
public class BagController {
@Autowired
private SchedualUploadFile schedualUploadFile;
@RequestMapping(value = "/upload" ,method = RequestMethod.POST)
public UploadFile upload(@RequestParam("file")MultipartFile file,@RequestParam("filePath") String filePath) {
UploadFile upload = schedualUploadFile.upload(file,filePath);
return upload;
}
总结:希望看了对你有所帮助。本人踩过坑 如 文件类型MultipartFile报错
如果有遇到同样问题 可以交流。