1.推荐一个开源的FastDFS客户端,支持最新的SpringBoot2.0。配置使用极为简单,支持连接池,支持自动生成缩略图

  1.1 在文件上传的微服务中 引入依赖

<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.</version>
</dependency>

  

  1.2引入配置启动类

@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}

  

  1.3配置Fast的属性

fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: # 缩略图
width: 60
height: 60
tracker-list: # tracker地址
- 192.168.56.101:22122

  

  1.4测试(测试的包名要和java中的包名一致哦)

@RunWith(SpringRunner.class)
@SpringBootTest
public class FdfsTest { @Autowired
private FastFileStorageClient storageClient; @Autowired
private ThumbImageConfig thumbImageConfig; @Test
public void testUpload() throws FileNotFoundException {
File file = new File("D:\\test\\baby.png");
// 上传并且生成缩略图
StorePath storePath = this.storageClient.uploadFile(
new FileInputStream(file), file.length(), "png", null);
// 带分组的路径
System.out.println(storePath.getFullPath());
// 不带分组的路径
System.out.println(storePath.getPath());
} @Test
public void testUploadAndCreateThumb() throws FileNotFoundException {
File file = new File("D:\\test\\baby.png");
// 上传并且生成缩略图
StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
new FileInputStream(file), file.length(), "png", null);
// 带分组的路径
System.out.println(storePath.getFullPath());
// 不带分组的路径
System.out.println(storePath.getPath());
// 获取缩略图路径
String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
System.out.println(path);
}
}

  配置上传文件大小的限制

#配置最大上传图片大小
client_max_body_size 10m;

  1.5测试成功

group1/M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630.png
M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630.png
M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630_60x60.png

2.修改文件上传Service

@Service
@Slf4j
//获得配置文件类
@EnableConfigurationProperties(UploadProperties.class)
public class UploadService { @Autowired
private UploadProperties properties;
@Autowired
private FastFileStorageClient storageClient;
//private static final List<String> ALLOW_TYPES= Arrays.asList("image/jpeg","image/png");
public String uploadImg(MultipartFile file) {
try {
//检验文件的类型 防止恶意文件
String contentType = file.getContentType();
if (!properties.getAllowTypes().contains(contentType)){
throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
};
//校验文件的内容
BufferedImage image = ImageIO.read(file.getInputStream());
if (image==null){
throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
}
//保存文件到本地
//File local = new File("F:\\javaee\\IdeaResource\\uploadImg\\",file.getOriginalFilename());
// file.transferTo(local); String suffix= StringUtils.substringAfterLast(file.getOriginalFilename(),".");
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), suffix, null); //返回文件地址
return properties.getBaseUrl()+storePath.getFullPath();
} catch (IOException e) {
log.error("[文件上传] 上传文件失败",e);
throw new LyException(ExceptionEnum.UPLOAD_FILE_ERROR);
}
}
}

  2.1配置文件类从 application.yaml中读取参数

@Data
//加载带有ly.upload前缀的配置文件属性
@ConfigurationProperties(prefix = "ly.upload")
public class UploadProperties { private String baseUrl;
private List<String> allowTypes;
}

  2.2配置文件application.yaml

server:
port: 8082
servlet:
session:
cookie:
http-only:
spring:
application:
name: upload-service
servlet:
multipart:
#最大上传文件
max-file-size: 5MB
#每次请求上传文件总和最大限制
max-request-size: 10MB
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
ip-address: 127.0.0.1
prefer-ip-address: true fdfs:
so-timeout: 2500
connect-timeout: 600
thumb-image: # 缩略图
width: 60
height: 60
tracker-list: # tracker地址
- 192.168.98.128:22122 ly:
upload:
baseUrl: http://image.leyou.com/
allowTypes:
- image/jpeg
- image/png
- image/bmp

  2.3会被读取到配置文件类中去

ly:
upload:
baseUrl: http://image.leyou.com/
allowTypes:
- image/jpeg
- image/png
- image/bmp

测试成功

最新文章

  1. 对抗假人 —— 前后端结合的 WAF
  2. visual studio code更新
  3. 表格不被内容撑大,且超出的内容变为省略号(css)
  4. RunLoop的模式
  5. .Net下一个类型转换神器
  6. scrapy3_ 安装指南
  7. Verilog之电平检测
  8. iOS开发中的错误整理,重写的构造函数中,没有通过self调用
  9. Menu菜单
  10. yii2的安装
  11. DBHerperl类
  12. IT行业新名词--透明手机/OCR(光学字符识别)/夹背电池
  13. PH日期格式化
  14. C++设计模式——访问者模式
  15. 使用ajax分页查询
  16. JustOj 1032: 习题6.7 完数
  17. 冲刺Two之站立会议1
  18. (2)bytes类型
  19. Java 8 新日期时间 API
  20. 2018.07.22 bzoj3613: [Heoi2014]南园满地堆轻絮(逆序对结论题)

热门文章

  1. 【转载】TCP拥塞控制算法 优缺点 适用环境 性能分析
  2. nginx的配置:目的是使用nginx反向代理后,应用程序获取用户真实ip
  3. 【转载】Delphi异常处理try except语句和try finally语句用法以及区别
  4. 手机端判断安卓,iso,微信
  5. 尚学python课程---11、linux环境下安装python注意
  6. Windbg 调试CPU占用过高
  7. day 69 Django基础五之django模型层(一)单表操作
  8. 小程序跳坑 --- navigator 和 API中wx.系列的跳转(如 wx.navigateTo、wx.reLaunch等)
  9. k8s 是什么,有什么功能
  10. 动态调整Log4j日志级别