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

+ 21 - 16
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/io/FileUtils.java

@@ -83,32 +83,37 @@ public class FileUtils {
         if (StrUtil.isNotBlank(originalName)) {
             String extName = FileNameUtil.extName(originalName);
             //return StrUtil.isBlank(extName) ? sha256Hex : sha256Hex + "." + extName;
-            return StrUtil.isBlank(extName) ? sha256Hex : DateUtil.format(new Date(), "yyyyMMDDHHmmss")+originalName;
+            return StrUtil.isBlank(extName) ? sha256Hex : originalName;
         }
         // 情况二:基于 content 计算
         return sha256Hex + '.' + FileTypeUtil.getType(new ByteArrayInputStream(content));
     }
 
-    public static double getFileSizeMB(String fileUrl){
-        HttpURLConnection conn = null;
+    public static long getFileSizeMB(String fileUrl){
         try {
             URL url = new URL(fileUrl);
-            conn = (HttpURLConnection) url.openConnection();
+            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             conn.setRequestMethod("HEAD");
-            int code = conn.getResponseCode();
-            if (code != HttpURLConnection.HTTP_OK) {
-                throw new IOException("HTTP错误码: " + code);
+            conn.setRequestProperty("Accept-Encoding", "identity"); // 禁用压缩
+
+            // 尝试解析Content-Length头
+            String contentLength = conn.getHeaderField("Content-Length");
+            if (contentLength != null) {
+                return Long.parseLong(contentLength);
             }
-            String length = conn.getHeaderField("Content-Length");
-            if (length == null) {
-                throw new IOException("Content-Length未找到");
+
+            // 处理分块传输或未知大小
+            if ("chunked".equalsIgnoreCase(conn.getHeaderField("Transfer-Encoding"))) {
+                System.out.println("分块传输编码,无法直接获取大小");
+                return -1;
             }
-            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();
+
+            conn.disconnect();
+            return -1; // 其他未知情况
+        } catch (IOException e) {
+            throw new RuntimeException(e);
         }
     }
+
+
 }

+ 6 - 2
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java

@@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.*;
 import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
 import cn.iocoder.yudao.module.infra.service.file.FileService;
+import com.google.common.collect.ImmutableMap;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
@@ -25,6 +26,8 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
 
+import java.util.Map;
+
 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 import static cn.iocoder.yudao.module.infra.framework.file.core.utils.FileTypeUtils.writeAttachment;
 
@@ -40,10 +43,11 @@ public class FileController {
 
     @PostMapping("/upload")
     @Operation(summary = "上传文件", description = "模式一:后端上传文件")
-    public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
+    public CommonResult<Map> uploadFile(FileUploadReqVO uploadReqVO) throws Exception {
         MultipartFile file = uploadReqVO.getFile();
+        long size = file.getSize();
         String path = uploadReqVO.getPath();
-        return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
+        return success(ImmutableMap.of("path",fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())),"size",size));
     }
 
     @GetMapping("/presigned-url")

+ 3 - 1
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/vo/IotInfoSaveReqVO.java

@@ -33,7 +33,7 @@ public class IotInfoSaveReqVO {
     private String fileType;
 
     @Schema(description = "文件路径", requiredMode = Schema.RequiredMode.REQUIRED)
-    @NotEmpty(message = "文件路径不能为空")
+//    @NotEmpty(message = "文件路径不能为空")
     private String filePath;
 
     @Schema(description = "文件大小")
@@ -42,4 +42,6 @@ public class IotInfoSaveReqVO {
     @Schema(description = "备注", example = "随便")
     private String remark;
 
+    @Schema(description = "文件列表")
+    private List<Map<String, String>> fileList;
 }

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

@@ -50,15 +50,16 @@ public class IotInfoServiceImpl implements IotInfoService {
     @Override
     public void createIotInfo(IotInfoSaveReqVO createReqVO) {
         // 插入
-        if (StringUtils.isBlank(createReqVO.getFilePath())) {
+        if (CollUtil.isEmpty(createReqVO.getFileList())) {
             throw exception(IOT_INFO_NOT_EXISTS);
         }
-        List<IotInfoDO> collect = Arrays.stream(createReqVO.getFilePath().split(",")).map(path -> {
+        List<IotInfoDO> collect = createReqVO.getFileList().stream().map(e -> {
             IotInfoDO iotInfo = BeanUtils.toBean(createReqVO, IotInfoDO.class);
-            // todo iotInfo.setFilename(StringUtils.substringAfterLast(path, File.separator));
-            iotInfo.setFilename(StringUtils.substringAfterLast(path, "/"));
-            iotInfo.setFilePath(path);
-            iotInfo.setFileSize(String.valueOf(FileUtils.getFileSizeMB(path)));
+            iotInfo.setFilename(StringUtils.substringAfterLast(e.get("url"), "/"));
+            iotInfo.setFilePath(e.get("url"));
+            long size = Long.parseLong(e.get("size"));
+            double mb = size / (1024.0 * 1024.0);
+            iotInfo.setFileSize(String.format("%.2f MB", mb));
             iotInfo.setDeleted(false);
             return iotInfo;
         }).collect(Collectors.toList());