|
@@ -1,6 +1,7 @@
|
|
|
package com.gis.common.util;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.aliyun.oss.OSSClient;
|
|
|
import com.aliyun.oss.model.OSSObject;
|
|
|
import com.aliyun.oss.model.OSSObjectSummary;
|
|
@@ -28,15 +29,11 @@ import java.util.Map;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
-public class AliyunOssUtil {
|
|
|
+public class AliYunOssUtil {
|
|
|
|
|
|
@Autowired
|
|
|
ConfigConstant configConstant;
|
|
|
|
|
|
-// @Autowired
|
|
|
-// FileUtils fileUtils;
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 初始化
|
|
|
*/
|
|
@@ -100,6 +97,51 @@ public class AliyunOssUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /***
|
|
|
+ * 直接上传字节,适合小文件, 以拼音命名,相同文字会覆盖
|
|
|
+ * @param file
|
|
|
+ * @param dir 存放目录 ,结尾需要带斜杠
|
|
|
+ * @param limitSize 限制文件大小,单位是Mb
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String, Object> uploadPinYingByByte(MultipartFile file, String dir, Integer limitSize) {
|
|
|
+
|
|
|
+ // 检查非法文件上传
|
|
|
+ boolean checkFile = FileUtils.checkFile(file);
|
|
|
+ if (!checkFile) {
|
|
|
+ throw new BaseRuntimeException("非法文件, 请重新上传");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断文件大小
|
|
|
+ if (limitSize != null){
|
|
|
+ long size = file.getSize()/1024;
|
|
|
+ if (size > limitSize) {
|
|
|
+ String msg = StrUtil.format("文件上传最大为{}MB", limitSize);
|
|
|
+ log.error(msg);
|
|
|
+ throw new BaseRuntimeException(msg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String pinyinName = RegexUtil.getPinyinName(originalFilename);
|
|
|
+ try {
|
|
|
+ String filePath = dir + pinyinName;
|
|
|
+ String ossPath = configConstant.ossBasePath + filePath;
|
|
|
+ this.upload(file.getBytes(), ossPath);
|
|
|
+ String ossUrl = configConstant.ossDomain + ossPath;
|
|
|
+ HashMap<String, Object> result = new HashMap<>();
|
|
|
+ result.put("fileName", originalFilename);
|
|
|
+ result.put("ossUrl", ossUrl);
|
|
|
+ result.put("ossPath", ossPath);
|
|
|
+ log.info("上传oss完成: {}", ossUrl);
|
|
|
+ return result;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
public void upload(String filePath, String key) {
|
|
|
OSSClient ossClient = init();
|