FastDFS:文件上传(工具类)

爱被打了一巴掌 2024-03-22 15:31 110阅读 0赞

FastDFS:文件上传(工具类)

依赖

  1. <!-- 高性能分布式文件服务器 -->
  2. <dependency>
  3. <groupId>com.github.tobato</groupId>
  4. <artifactId>fastdfs-client</artifactId>
  5. <version>1.26.2</version>
  6. </dependency>

配置文件application.properties

  1. fdfs.soTimeout=1501
  2. fdfs.connectTimeout=601
  3. fdfs.thumbImage.width=80
  4. fdfs.thumbImage.height=80
  5. fdfs.trackerList[0]=192.168.1.70:22122

代码

  1. import java.io.ByteArrayInputStream;
  2. import java.io.IOException;
  3. import java.nio.charset.Charset;
  4. import org.apache.commons.io.FilenameUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.web.multipart.MultipartFile;
  9. import com.github.tobato.fastdfs.domain.StorePath;
  10. import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
  11. import com.github.tobato.fastdfs.service.FastFileStorageClient;
  12. @Component
  13. public class FastDFSClient {
  14. @Autowired
  15. private FastFileStorageClient storageClient;
  16. // @Autowired
  17. // private AppConfig appConfig; // 项目参数配置
  18. /**
  19. * 上传文件
  20. *
  21. * @param file
  22. * 文件对象
  23. * @return 文件访问地址
  24. * @throws IOException
  25. */
  26. public String uploadFile(MultipartFile file) throws IOException {
  27. StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
  28. FilenameUtils.getExtension(file.getOriginalFilename()), null);
  29. return storePath.getPath();
  30. }
  31. public String uploadFile2(MultipartFile file) throws IOException {
  32. StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
  33. FilenameUtils.getExtension(file.getOriginalFilename()), null);
  34. return storePath.getPath();
  35. }
  36. public String uploadQRCode(MultipartFile file) throws IOException {
  37. StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
  38. "png", null);
  39. return storePath.getPath();
  40. }
  41. public String uploadFace(MultipartFile file) throws IOException {
  42. StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
  43. "png", null);
  44. return storePath.getPath();
  45. }
  46. public String uploadBase64(MultipartFile file) throws IOException {
  47. StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
  48. "png", null);
  49. return storePath.getPath();
  50. }
  51. /**
  52. * 将一段字符串生成一个文件上传
  53. *
  54. * @param content
  55. * 文件内容
  56. * @param fileExtension
  57. * @return
  58. */
  59. public String uploadFile(String content, String fileExtension) {
  60. byte[] buff = content.getBytes(Charset.forName("UTF-8"));
  61. ByteArrayInputStream stream = new ByteArrayInputStream(buff);
  62. StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
  63. return storePath.getPath();
  64. }
  65. // 封装图片完整URL地址
  66. // private String getResAccessUrl(StorePath storePath) {
  67. // String fileUrl = AppConstants.HTTP_PRODOCOL + appConfig.getResHost() + ":" + appConfig.getFdfsStoragePort()
  68. // + "/" + storePath.getFullPath();
  69. // return fileUrl;
  70. // }
  71. /**
  72. * 删除文件
  73. *
  74. * @param fileUrl
  75. * 文件访问地址
  76. * @return
  77. */
  78. public void deleteFile(String fileUrl) {
  79. if (StringUtils.isEmpty(fileUrl)) {
  80. return;
  81. }
  82. try {
  83. StorePath storePath = StorePath.praseFromUrl(fileUrl);
  84. storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
  85. } catch (FdfsUnsupportStorePathException e) {
  86. e.getMessage();
  87. }
  88. }
  89. }

发表评论

表情:
评论列表 (有 0 条评论,110人围观)

还没有评论,来说两句吧...

相关阅读