springboot:整合fastdfs

x33g5p2x  于2022-04-27 转载在 Spring  
字(5.9k)|赞(0)|评价(0)|浏览(546)

springboot:整合fastdfs

一、部署fastdfs

docker部署fastdfs

二、集成

依赖

  1. <!-- FastDFS依赖-->
  2. <dependency>
  3. <groupId>com.github.tobato</groupId>
  4. <artifactId>fastdfs-client</artifactId>
  5. <version>1.27.2</version>
  6. </dependency>

配置类

  1. @Configuration
  2. //拥有带有连接池的FastDFS Java客户端
  3. @Import(FdfsClientConfig.class)
  4. //解决JMX重复注册bean的问题
  5. @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
  6. public class FastDFSClientConfig {
  7. }

yml配置

  1. fdfs:
  2. reqHost: 127.0.0.1
  3. reqPort: 8899
  4. so-timeout: 3000 #socket连接超时时长
  5. connect-timeout: 1000 #连接tracker服务器超时时长
  6. thumb-image: # 略图生成参数,可选
  7. width: 60
  8. height: 60
  9. tracker-list: #TrackerList参数,支持多个,在下方加- ip:port
  10. - 127.0.0.1:22122

工具类

  1. package com.yolo.yoloblog.util;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.github.tobato.fastdfs.domain.fdfs.MetaData;
  4. import com.github.tobato.fastdfs.domain.fdfs.StorePath;
  5. import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
  6. import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
  7. import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
  8. import com.github.tobato.fastdfs.service.FastFileStorageClient;
  9. import lombok.Data;
  10. import org.apache.commons.io.FilenameUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import java.io.*;
  16. import java.nio.charset.StandardCharsets;
  17. import java.util.List;
  18. import java.util.Set;
  19. import java.util.zip.ZipEntry;
  20. import java.util.zip.ZipOutputStream;
  21. @Component
  22. @Data
  23. public class FastDFSUtil {
  24. @Autowired
  25. private FastFileStorageClient storageClient;
  26. //缩略图处理
  27. @Autowired
  28. private ThumbImageConfig thumbImageConfig;
  29. //fastDFS用nginx的连接
  30. @Value("${fdfs.reqHost}")
  31. private String reqHost;
  32. //fastDFS用nginx的连接
  33. @Value("${fdfs.reqPort}")
  34. private String reqPort;
  35. /**
  36. * MultipartFile类型的文件上传ַ
  37. * @param file
  38. * @return fastDfs文件保存路径
  39. * @throws IOException
  40. */
  41. public String uploadFile(MultipartFile file) throws IOException {
  42. StorePath path = storageClient.uploadFile(file.getInputStream(), file.getSize(),
  43. FilenameUtils.getExtension(file.getOriginalFilename()), null);
  44. return path.getFullPath();
  45. }
  46. /**
  47. * 普通的文件上传
  48. * @param file
  49. * @return fastDfs文件保存路径
  50. * @throws IOException
  51. */
  52. public String uploadFile(File file) throws IOException {
  53. FileInputStream inputStream = new FileInputStream(file);
  54. StorePath path = storageClient.uploadFile(inputStream, file.length(),
  55. FilenameUtils.getExtension(file.getName()), null);
  56. return path.getFullPath();
  57. }
  58. /**
  59. * 带输入流形式的文件上传
  60. * @param is 输入流
  61. * @param fileSize 文件大小
  62. * @param fileExtName 文件拓展名
  63. * @return fastDfs文件保存路径
  64. */
  65. public String uploadFileStream(InputStream is, long fileSize, String fileExtName) {
  66. StorePath path = storageClient.uploadFile(is, fileSize, fileExtName, null);
  67. return path.getFullPath();
  68. }
  69. /**
  70. * 文件上传
  71. * @param bytes 文件字节
  72. * @param fileSize 文件大小
  73. * @param fileExtName 文件扩展名
  74. * @return fastDfs文件保存路径
  75. */
  76. public String uploadFile(byte[] bytes, long fileSize, String fileExtName) {
  77. ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
  78. StorePath storePath = storageClient.uploadFile(byteArrayInputStream, fileSize, fileExtName, null);
  79. return storePath.getFullPath();
  80. }
  81. /**
  82. * 将一段文本文件写到fastdfs的服务器上
  83. * @param content 文本内容
  84. * @param fileExtName 文件扩展名
  85. * @return fastDfs文件保存路径
  86. */
  87. public String uploadFile(String content, String fileExtName) {
  88. byte[] buff = content.getBytes(StandardCharsets.UTF_8);
  89. ByteArrayInputStream stream = new ByteArrayInputStream(buff);
  90. StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtName, null);
  91. return storePath.getFullPath();
  92. }
  93. /**
  94. * 上传图片文件和缩略图
  95. * @param is 输入流
  96. * @param size 文件大小
  97. * @param fileExtName 文件扩展名
  98. * @param metaData 可以为null
  99. * @return 原始图片地址和缩略图地址
  100. */
  101. public String[] upFileImage(InputStream is, long size, String fileExtName, Set<MetaData> metaData) {
  102. StorePath storePath = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
  103. String imagePath = storePath.getFullPath();
  104. String thumbImagePath = thumbImageConfig.getThumbImagePath(imagePath);
  105. return new String[]{imagePath,thumbImagePath};
  106. }
  107. /**
  108. * 删除文件
  109. * @param fileUrl
  110. */
  111. public void deleteFile(String fileUrl) {
  112. if (StrUtil.isEmpty(fileUrl)) {
  113. return;
  114. }
  115. try{
  116. StorePath storePath = StorePath.parseFromUrl(fileUrl);
  117. storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
  118. } catch (FdfsUnsupportStorePathException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. /**
  123. * 下载文件
  124. * @param fileUrl
  125. */
  126. public byte[] downloadFile(String fileUrl) {
  127. if (StrUtil.isEmpty(fileUrl)) {
  128. return null;
  129. }
  130. StorePath storePath = StorePath.parseFromUrl(fileUrl);
  131. return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
  132. }
  133. /**
  134. * 下载文件
  135. * @param fileUrl
  136. */
  137. public InputStream download(String fileUrl ) {
  138. if (StrUtil.isEmpty(fileUrl)) {
  139. return null;
  140. }
  141. StorePath storePath = StorePath.parseFromUrl(fileUrl);
  142. return storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), (InputStream ins)->{
  143. // 将此ins返回给上面的inputStream
  144. return ins;
  145. });
  146. }
  147. /**
  148. * 批量下载文件
  149. * @param fileUrlList
  150. */
  151. public InputStream downloadFile(List<String> fileUrlList){
  152. byte[] buffer = new byte[1024];
  153. // 创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量
  154. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  155. //创建一个新的缓冲输出流,以将数据写入指定的底层输出流
  156. BufferedOutputStream fos = new BufferedOutputStream(baos);
  157. ZipOutputStream zos = new ZipOutputStream(fos);
  158. try{
  159. for (String fileUrl:fileUrlList){
  160. if (StrUtil.isNotEmpty(fileUrl)) {
  161. StorePath storePath = StorePath.parseFromUrl(fileUrl);
  162. byte[] bytes = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray()) ;
  163. //压缩文件内的文件名称
  164. String fileName = StrUtil.subSuf(fileUrl,fileUrl.lastIndexOf("/")+1);
  165. zos.putNextEntry(new ZipEntry(fileName));
  166. zos.write(bytes);
  167. zos.closeEntry();
  168. }
  169. }
  170. zos.close();
  171. fos.flush();
  172. InputStream is = new ByteArrayInputStream(baos.toByteArray());
  173. return is;
  174. }catch (Exception e){
  175. e.printStackTrace();
  176. }
  177. return null;
  178. }
  179. /**
  180. * 封装文件通过浏览器直接访问文件的地址
  181. * @param fileUrl
  182. * @return
  183. */
  184. public String getViewAccessUrl(String fileUrl) {
  185. return "http://" + reqHost + ":" + reqPort + "/" + fileUrl;
  186. }
  187. }

控制器

  1. @PostMapping("/upload")
  2. public String upload(@RequestParam("file") MultipartFile file) throws IOException {
  3. String path = fastDFSUtil.uploadFile(file);
  4. return fastDFSUtil.getViewAccessUrl(path);
  5. }

相关文章