FastDfsSysFileServiceImpl.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.ruoyi.file.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.web.multipart.MultipartFile;
  6. import com.github.tobato.fastdfs.domain.fdfs.StorePath;
  7. import com.github.tobato.fastdfs.service.FastFileStorageClient;
  8. import com.ruoyi.common.core.utils.file.FileTypeUtils;
  9. import java.io.InputStream;
  10. /**
  11. * FastDFS 文件存储
  12. *
  13. * @author ruoyi
  14. */
  15. @Service
  16. public class FastDfsSysFileServiceImpl implements ISysFileService
  17. {
  18. /**
  19. * 域名或本机访问地址
  20. */
  21. @Value("${fdfs.domain}")
  22. public String domain;
  23. @Autowired
  24. private FastFileStorageClient storageClient;
  25. /**
  26. * FastDfs文件上传接口
  27. *
  28. * @param file 上传的文件
  29. * @return 访问地址
  30. * @throws Exception
  31. */
  32. @Override
  33. public String uploadFile(MultipartFile file) throws Exception
  34. {
  35. InputStream inputStream = file.getInputStream();
  36. StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(),
  37. FileTypeUtils.getExtension(file), null);
  38. inputStream.close();
  39. return domain + "/" + storePath.getFullPath();
  40. }
  41. }