SpringBoot2.0整合Fastdfs

左手的ㄟ右手 2023-05-29 06:23 64阅读 0赞

接上一篇,docker搭建FastDFS文件系统。

这一篇实现SpringBoot2.0整合Fastdfs。

1.引入FastDfs依赖

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

2.application.yml配置文件

  1. fdfs:
  2. connect-timeout: 600
  3. so-timeout: 1500
  4. trackerList: 127.0.0.1:22122
  5. thumb-image:
  6. width: 150
  7. height: 150
  8. pool:
  9. max-total: 200

3.在启动类添加如下配置

  1. @Configuration
  2. @SpringBootApplication
  3. @MapperScan("com.gbq.boot.web.mapper")
  4. @EnableMBeanExport(registration= RegistrationPolicy.IGNORE_EXISTING)
  5. @Import(FdfsClientConfig.class)
  6. @EnableAsync
  7. public class BootApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(BootApplication.class, args);
  10. }
  11. }

4.controller 接口编写

  1. package com.gbq.boot.web.controller;
  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.velocity.shaded.commons.io.FilenameUtils;
  6. import org.springframework.web.bind.annotation.*;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import javax.annotation.Resource;
  9. import javax.servlet.ServletOutputStream;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12. import java.net.URLEncoder;
  13. import java.util.HashMap;
  14. @RestController
  15. @RequestMapping("/file")
  16. public class FileController {
  17. @Resource
  18. private FastFileStorageClient fastFileStorageClient;
  19. /**
  20. * 文件上传
  21. * @return result
  22. */
  23. @ResponseBody
  24. @PostMapping("/upload")
  25. public HashMap<String, Object> uploadImageByCover(MultipartFile attach){
  26. HashMap<String,Object> result = new HashMap<>();
  27. try {
  28. //上传
  29. StorePath path = fastFileStorageClient.
  30. uploadFile(attach.getInputStream(),attach.getSize(),
  31. FilenameUtils.getExtension(attach.getOriginalFilename()),null);
  32. //获取路径加名称
  33. String picName = path.getFullPath();
  34. result.put("msg",picName);
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. return result;
  39. }
  40. /**
  41. * 文件删除
  42. * @param path
  43. * @return
  44. */
  45. @DeleteMapping("/delete")
  46. public HashMap<String, Object> delete(@RequestParam String path) {
  47. HashMap<String,Object> result = new HashMap<>();
  48. // 第一种删除:参数:完整地址
  49. fastFileStorageClient.deleteFile(path);
  50. result.put("msg","恭喜恭喜,删除成功!");
  51. // 第二种删除:参数:组名加文件路径
  52. // fastFileStorageClient.deleteFile(group,path);
  53. return result;
  54. }
  55. /**
  56. * 文件下载
  57. * @param url 路径
  58. * @return
  59. */
  60. @GetMapping("/download")
  61. public void downLoad(@RequestParam String url, HttpServletResponse response) throws IOException {
  62. String group = url.substring(0, url.indexOf("/"));
  63. String path = url.substring(url.indexOf("/") + 1);
  64. //文件后缀
  65. String substring = url.substring(url.lastIndexOf(".") + 1);
  66. byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
  67. response.setCharacterEncoding("UTF-8");
  68. response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(UUID.randomUUID().toString()+"."+substring, "UTF-8"));
  69. // 写出
  70. ServletOutputStream outputStream = response.getOutputStream();
  71. IOUtils.write(bytes, outputStream);
  72. }
  73. }

测试一下!!!!

上传!

format_png

浏览器访问一下

format_png 1

删除!把之前的照片删掉!

format_png 2

再通过浏览器访问,应该就是404,记得清除浏览器缓存哦!

format_png 3

下载!记得再上传一张照片,如果服务器有照片请忽略!

format_png 4

注意一下上传路径,fastFileStorageClient会加上你的服务器地址+ip,只需要刚刚上传成功路径即可!

ok,全部都大功告成了,拜拜!有啥问题群里联系我!

发表评论

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

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

相关阅读

    相关 springboot整合fastdfs

    在项目开发中经常会碰到做文件上传的功能,一般来说,文件上传的步骤就那么几步,前台通过提交一个选中的文件,后端对文件做处理然后将文件上传至指定的地址,这个地址是一个真实的物理存储