CloudStorageService.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.xjrsoft.common.factory;
  2. import com.xjrsoft.common.utils.DateUtils;
  3. import com.xjrsoft.config.OSSConfig;
  4. import org.apache.commons.lang.StringUtils;
  5. import java.io.InputStream;
  6. import java.util.Date;
  7. import java.util.UUID;
  8. /**
  9. * 云存储(支持七牛、阿里云、腾讯云、华为)
  10. *
  11. * @author tzx
  12. */
  13. public abstract class CloudStorageService {
  14. /** 云存储配置信息 */
  15. OSSConfig config;
  16. /**
  17. * 文件路径
  18. * @param prefix 前缀
  19. * @param suffix 后缀
  20. * @return 返回上传路径
  21. */
  22. public String getPath(String prefix, String suffix) {
  23. //生成uuid
  24. String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  25. //文件路径
  26. String path = DateUtils.format(new Date(), "yyyyMMdd") + "/" + uuid;
  27. if(StringUtils.isNotBlank(prefix)){
  28. path = prefix + "/" + path;
  29. }
  30. return path + suffix;
  31. }
  32. /**
  33. * 文件上传
  34. * @param data 文件字节数组
  35. * @param path 文件路径,包含文件名
  36. * @return 返回http地址
  37. */
  38. public abstract String upload(byte[] data, String path);
  39. /**
  40. * 文件上传
  41. * @param data 文件字节数组
  42. * @param suffix 后缀
  43. * @return 返回http地址
  44. */
  45. public abstract String uploadSuffix(byte[] data, String suffix);
  46. /**
  47. * 文件上传
  48. * @param inputStream 字节流
  49. * @param path 文件路径,包含文件名
  50. * @return 返回http地址
  51. */
  52. public abstract String upload(InputStream inputStream, String path);
  53. /**
  54. * 文件上传
  55. * @param inputStream 字节流
  56. * @param suffix 后缀
  57. * @return 返回http地址
  58. */
  59. public abstract String uploadSuffix(InputStream inputStream, String suffix);
  60. public abstract boolean delete(String objectName );
  61. }