上传的图片文件比较大,不用走网关,需要在ly-gateway中忽略掉网关的配置:
routes:
item-service: /item/** #商品微服务
search-service: /search/** #搜索微服务
user-service: /user/** #用户微服务
auth-service: /auth/** #授权中心微服务
cart-service: /cart/** #购物车微服务
order-service: /order/** #订单微服务
ignored-services:
- upload-service # 忽略upload-service服务
ignored-services:就是忽略的服务
上传的微服模块ly-upload(使用FastDFS来进行上传)
application.yml文件的相关的配置:
- 可以限定上传的文件的大小 max-file-size
- 需要配置上传服务器的地址 traker-list:服务器的地址
- 可以设置上传和thumb缩略图片的大小
server:
port: 8082
spring:
application:
name: upload-service
servlet:
multipart:
max-file-size: 10MB # 限制文件上传的大小
# Eureka
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
prefer-ip-address: true
ip-address: 127.0.0.1
instance-id: ${
spring.application.name}:${
server.port}
# 图片服务器的配置
fdfs:
so-timeout: 1501 #超时时间
connect-timeout: 601 #连接超时时间
thumb-image: # 缩略图的大小配置
width: 60
height: 60
tracker-list: # tracker地址
- 192.168.19.121:22122
UploadServiceimpl实现:
- 注入FastFileStorageClient
- 使用MutipartFile 这个二进制流进行上传
package com.leyou.upload.service.serviceimpl;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.leyou.upload.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.List;
/**
* @Author: 98050
* Time: 2018-08-09 14:44
* Feature:
*/
@Service
public class UploadServiceImpl implements UploadService {
@Autowired
private FastFileStorageClient storageClient;
private static final Logger logger= LoggerFactory.getLogger(UploadServiceImpl.class);
/**
* 支持上传的文件类型
*/
private static final List<String> suffixes = Arrays.asList("image/png","image/jpeg","image/jpg");
@Override
public String upload(MultipartFile file) {
/**
* 1.图片信息校验
* 1)校验文件类型
* 2)校验图片内容
* 2.保存图片
* 1)生成保存目录
* 2)保存图片
* 3)拼接图片地址
*/
try {
String type = file.getContentType();
if (!suffixes.contains(type)) {
logger.info("上传文件失败,文件类型不匹配:{}", type);
return null;
}
BufferedImage image = ImageIO.read(file.getInputStream());
if (image == null) {
logger.info("上传失败,文件内容不符合要求");
return null;
}
// File dir = new File("G:\\LeYou\\upload");
// if (!dir.exists()){
// dir.mkdirs();
// }
// file.transferTo(new File(dir, Objects.requireNonNull(file.getOriginalFilename())));
StorePath storePath = this.storageClient.uploadFile(
file.getInputStream(), file.getSize(), getExtension(file.getOriginalFilename()), null);
//String url = "http://image.leyou.com/upload/"+file.getOriginalFilename();
String url = "http://image.leyou.com/"+storePath.getFullPath();
// System.out.println(url);
return url;
}catch (Exception e){
return null;
}
}
public String getExtension(String fileName){
return StringUtils.substringAfterLast(fileName,".");
}
}
需要配置一个config配置文件:
package com.leyou.config;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
@Configuration
@Import(FdfsClientConfig.class)
/**
* @author li
* 解决jmx重复注册bean的问题
*/
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}
一个controller进行相关的上传:
package com.leyou.upload.controller;
import com.leyou.upload.service.serviceimpl.UploadServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* @Author: 98050
* Time: 2018-08-09 14:36
* Feature:
*/
@RestController
@RequestMapping("upload")
public class UploadController {
@Autowired
private UploadServiceImpl uploadServiceImpl;
/**
* 图片上传
* @param file
* @return
*/
@PostMapping("image")
public ResponseEntity<String> uploadImage(@RequestParam("file")MultipartFile file){
String url= this.uploadServiceImpl.upload(file);
if(StringUtils.isBlank(url)){
//url为空,证明上传失败
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
return ResponseEntity.ok(url);
}
}