Java SpringBoot FastDFS

添加FastDFS的相关依赖

  1. <dependency>
  2. <groupId>com.github.tobato</groupId>
  3. <artifactId>fastdfs-client</artifactId>
  4. <version>1.26.2</version>
  5. </dependency>

在springboot启动类上添加导入配置类的注解

  1. @Import(FdfsClientConfig.class)
  2. @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)

创建FastDFSClient工具类

  1. import com.github.tobato.fastdfs.conn.FdfsWebServer;
  2. import com.github.tobato.fastdfs.domain.StorePath;
  3. import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
  4. import com.github.tobato.fastdfs.service.FastFileStorageClient;
  5. import org.apache.commons.io.FilenameUtils;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.multipart.MultipartFile;
  12. import java.io.*;
  13. @Component
  14. public class FastDFSClient {
  15. private static Logger log =LoggerFactory.getLogger(FastDFSClient.class);
  16. private static FastFileStorageClient fastFileStorageClient;
  17. private static FdfsWebServer fdfsWebServer;
  18. @Autowired
  19. public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
  20. FastDFSClient.fastFileStorageClient = fastFileStorageClient;
  21. FastDFSClient.fdfsWebServer = fdfsWebServer;
  22. }
  23. /**
  24. * @param multipartFile 文件对象
  25. * @return 返回文件地址
  26. * @author qbanxiaoli
  27. * @description 上传文件
  28. */
  29. public static String uploadFile(MultipartFile multipartFile) {
  30. try {
  31. StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
  32. return storePath.getFullPath();
  33. } catch (IOException e) {
  34. log.error(e.getMessage());
  35. return null;
  36. }
  37. }
  38. /**
  39. * @param multipartFile 图片对象
  40. * @return 返回图片地址
  41. * @author Fcant
  42. * @description 上传缩略图
  43. */
  44. public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
  45. try {
  46. StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
  47. return storePath.getFullPath();
  48. } catch (Exception e) {
  49. log.error(e.getMessage());
  50. return null;
  51. }
  52. }
  53. /**
  54. * @param file 文件对象
  55. * @return 返回文件地址
  56. * @author Fcant
  57. * @description 上传文件
  58. */
  59. public static String uploadFile(File file) {
  60. try {
  61. FileInputStream inputStream = new FileInputStream(file);
  62. StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
  63. return storePath.getFullPath();
  64. } catch (Exception e) {
  65. log.error(e.getMessage());
  66. return null;
  67. }
  68. }
  69. /**
  70. * @param file 图片对象
  71. * @return 返回图片地址
  72. * @author Fcant
  73. * @description 上传缩略图
  74. */
  75. public static String uploadImageAndCrtThumbImage(File file) {
  76. try {
  77. FileInputStream inputStream = new FileInputStream(file);
  78. StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
  79. return storePath.getFullPath();
  80. } catch (Exception e) {
  81. log.error(e.getMessage());
  82. return null;
  83. }
  84. }
  85. /**
  86. * @param bytes byte数组
  87. * @param fileExtension 文件扩展名
  88. * @return 返回文件地址
  89. * @author Fcant
  90. * @description 将byte数组生成一个文件上传
  91. */
  92. public static String uploadFile(byte[] bytes, String fileExtension) {
  93. ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
  94. StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
  95. return storePath.getFullPath();
  96. }
  97. /**
  98. * @param fileUrl 文件访问地址
  99. * @param file 文件保存路径
  100. * @author Fcant
  101. * @description 下载文件
  102. */
  103. public static boolean downloadFile(String fileUrl, File file) {
  104. try {
  105. StorePath storePath = StorePath.praseFromUrl(fileUrl);
  106. byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
  107. FileOutputStream stream = new FileOutputStream(file);
  108. stream.write(bytes);
  109. } catch (Exception e) {
  110. log.error(e.getMessage());
  111. return false;
  112. }
  113. return true;
  114. }
  115. /**
  116. * @param fileUrl 文件访问地址
  117. * @author Fcant
  118. * @description 删除文件
  119. */
  120. public static boolean deleteFile(String fileUrl) {
  121. if (StringUtils.isEmpty(fileUrl)) {
  122. return false;
  123. }
  124. try {
  125. StorePath storePath = StorePath.praseFromUrl(fileUrl);
  126. fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
  127. } catch (Exception e) {
  128. log.error(e.getMessage());
  129. return false;
  130. }
  131. return true;
  132. }
  133. // 封装文件完整URL地址
  134. public static String getResAccessUrl(String path) {
  135. String url = fdfsWebServer.getWebServerUrl() + path;
  136. log.info("上传文件地址为:\n" + url);
  137. return url;
  138. }
  139. }

配置yml文件

  1. # 分布式文件系统fastdfs配置
  2. fdfs:
  3. # socket连接超时时长
  4. soTimeout: 1500
  5. # 连接tracker服务器超时时长
  6. connectTimeout: 600
  7. pool:
  8. # 从池中借出的对象的最大数目
  9. max-total: 153
  10. # 获取连接时的最大等待毫秒数100
  11. max-wait-millis: 102
  12. # 缩略图生成参数,可选
  13. thumbImage:
  14. width: 150
  15. height: 150
  16. # 跟踪服务器tracker_server请求地址,支持多个,这里只有一个,如果有多个在下方加- x.x.x.x:port
  17. trackerList:
  18. - 192.168.127.131:22122
  19. #
  20. # 存储服务器storage_server访问地址
  21. web-server-url: http://192.168.127.131/
  22. spring:
  23. http:
  24. multipart:
  25. max-file-size: 100MB # 最大支持文件大小
  26. max-request-size: 100MB # 最大支持请求大小

测试类

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class FileClientApplicationTests {
  4. @Test
  5. public void Upload() {
  6. String fileUrl = this.getClass().getResource("/test.jpg").getPath();
  7. File file = new File(fileUrl);
  8. String str = FastDFSClient.uploadFile(file);
  9. FastDFSClient.getResAccessUrl(str);
  10. }
  11. @Test
  12. public void Delete() {
  13. FastDFSClient.deleteFile("group1/M00/00/00/rBEAClu8OiSAFbN_AAbhXQnXzvw031.jpg");
  14. }
  15. }

运行测试类,返回的就是能访问文件的URL地址。