LocalSysFileServiceImpl.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.ruoyi.file.service;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.context.annotation.Primary;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.web.multipart.MultipartFile;
  6. import com.ruoyi.common.core.utils.StringUtils;
  7. import com.ruoyi.common.core.utils.file.FileUtils;
  8. import com.ruoyi.file.utils.FileUploadUtils;
  9. /**
  10. * 本地文件存储
  11. *
  12. * @author ruoyi
  13. */
  14. @Primary
  15. @Service
  16. public class LocalSysFileServiceImpl implements ISysFileService
  17. {
  18. /**
  19. * 资源映射路径 前缀
  20. */
  21. @Value("${file.prefix}")
  22. public String localFilePrefix;
  23. /**
  24. * 域名或本机访问地址
  25. */
  26. @Value("${file.domain}")
  27. public String domain;
  28. /**
  29. * 上传文件存储在本地的根路径
  30. */
  31. @Value("${file.path}")
  32. private String localFilePath;
  33. /**
  34. * 本地文件上传接口
  35. *
  36. * @param file 上传的文件
  37. * @return 访问地址
  38. * @throws Exception
  39. */
  40. @Override
  41. public String uploadFile(MultipartFile file) throws Exception
  42. {
  43. String name = FileUploadUtils.upload(localFilePath, file);
  44. String url = domain + localFilePrefix + name;
  45. return url;
  46. }
  47. /**
  48. * 本地文件删除接口
  49. *
  50. * @param fileUrl 文件访问URL
  51. * @throws Exception
  52. */
  53. @Override
  54. public void deleteFile(String fileUrl) throws Exception
  55. {
  56. String localFile = StringUtils.substringAfter(fileUrl, localFilePrefix);
  57. FileUtils.deleteFile(localFilePath + localFile);
  58. }
  59. }