lipenghui 4 місяців тому
батько
коміт
c96b5c99f2

+ 1 - 1
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/exception/enums/GlobalErrorCodeConstants.java

@@ -13,7 +13,7 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode;
  * @author 芋道源码
  */
 public interface GlobalErrorCodeConstants {
-
+    ErrorCode FILE_SIZE_ERROR = new ErrorCode(126,"获取文件大小错误");
     ErrorCode SUCCESS = new ErrorCode(0, "成功");
 
     // ========== 客户端错误段 ==========

+ 27 - 0
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/io/FileUtils.java

@@ -7,10 +7,15 @@ import cn.hutool.core.io.file.FileNameUtil;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.crypto.digest.DigestUtil;
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
+import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
 import lombok.SneakyThrows;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.util.Date;
 
 /**
@@ -84,4 +89,26 @@ public class FileUtils {
         return sha256Hex + '.' + FileTypeUtil.getType(new ByteArrayInputStream(content));
     }
 
+    public static double getFileSizeMB(String fileUrl){
+        HttpURLConnection conn = null;
+        try {
+            URL url = new URL(fileUrl);
+            conn = (HttpURLConnection) url.openConnection();
+            conn.setRequestMethod("HEAD");
+            int code = conn.getResponseCode();
+            if (code != HttpURLConnection.HTTP_OK) {
+                throw new IOException("HTTP错误码: " + code);
+            }
+            String length = conn.getHeaderField("Content-Length");
+            if (length == null) {
+                throw new IOException("Content-Length未找到");
+            }
+            long bytes = Long.parseLong(length);
+            return bytes / (1024.0 * 1024.0);
+        } catch (Exception e){
+            throw new  ServiceException(GlobalErrorCodeConstants.FILE_SIZE_ERROR);
+        } finally {
+            if (conn != null) conn.disconnect();
+        }
+    }
 }

+ 6 - 0
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/service/IotInfoServiceImpl.java

@@ -1,6 +1,8 @@
 package cn.iocoder.yudao.module.pms.service;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.iocoder.yudao.framework.common.exception.ServiceException;
+import cn.iocoder.yudao.framework.common.util.io.FileUtils;
 import cn.iocoder.yudao.module.pms.controller.admin.vo.IotInfoPageReqVO;
 import cn.iocoder.yudao.module.pms.controller.admin.vo.IotInfoSaveReqVO;
 import cn.iocoder.yudao.module.pms.dal.dataobject.IotInfoClassifyDO;
@@ -16,6 +18,7 @@ import org.springframework.validation.annotation.Validated;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -55,6 +58,7 @@ public class IotInfoServiceImpl implements IotInfoService {
             // todo iotInfo.setFilename(StringUtils.substringAfterLast(path, File.separator));
             iotInfo.setFilename(StringUtils.substringAfterLast(path, "/"));
             iotInfo.setFilePath(path);
+            iotInfo.setFileSize(String.valueOf(FileUtils.getFileSizeMB(path)));
             iotInfo.setDeleted(false);
             return iotInfo;
         }).collect(Collectors.toList());
@@ -69,6 +73,8 @@ public class IotInfoServiceImpl implements IotInfoService {
         // 更新
         IotInfoDO updateObj = BeanUtils.toBean(updateReqVO, IotInfoDO.class);
         updateObj.setFilename(StringUtils.substringAfterLast(updateObj.getFilePath(), "/"));
+        updateObj.setFilePath(updateReqVO.getFilePath());
+        updateObj.setFileSize(String.valueOf(FileUtils.getFileSizeMB(updateReqVO.getFilePath())));
         iotInfoMapper.updateById(updateObj);
     }