Browse Source

图片上传2

jichaobo 3 năm trước cách đây
mục cha
commit
68553b2ea5

+ 27 - 16
fiveep-service/src/main/java/com/bizmatics/service/impl/PatrolInspectionDeviceServiceImpl.java

@@ -1,5 +1,9 @@
 package com.bizmatics.service.impl;
 
+import com.bizmatics.common.core.exception.BusinessCheckException;
+import com.bizmatics.common.core.exception.BusinessException;
+import com.bizmatics.common.core.util.FileUtils;
+import com.bizmatics.common.spring.util.GlobalUtils;
 import com.bizmatics.model.PatrolInspectionDevice;
 import com.bizmatics.model.system.SysUser;
 import com.bizmatics.model.vo.PatrolInspectionDeviceVo;
@@ -67,24 +71,31 @@ public class PatrolInspectionDeviceServiceImpl extends AbstractCrudService<Patro
             System.out.println("文件为空空");
         }
 
-        String fileName = file.getOriginalFilename();  // 文件名
-        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
-//        String filePath = "D://temp-rainy//"; // 上传后的路径
-        fileName = UUID.randomUUID() + suffixName; // 新文件名
-
-        String destFileName = request.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;
-        File dest = new File(destFileName);
-//        File dest = new File(filePath + fileName);
-        if (!dest.getParentFile().exists()) {
-            dest.getParentFile().mkdirs();
-        }
+        File file1 = FileUtils.getFile(GlobalUtils.getTempBaseDir(), file.getOriginalFilename());
         try {
-            file.transferTo(dest);
-        } catch (IOException e) {
-            e.printStackTrace();
+            file.transferTo(file1);
+        }catch (Exception e){
+            throw new BusinessException("文件上传异常", e);
         }
-        String filename = "/uploaded/" + fileName;
-        return filename;
+
+//        String fileName = file.getOriginalFilename();  // 文件名
+//        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
+////        String filePath = "D://temp-rainy//"; // 上传后的路径
+//        fileName = UUID.randomUUID() + suffixName; // 新文件名
+//
+//        String destFileName = request.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;
+//        File dest = new File(destFileName);
+////        File dest = new File(filePath + fileName);
+//        if (!dest.getParentFile().exists()) {
+//            dest.getParentFile().mkdirs();
+//        }
+//        try {
+//            file.transferTo(dest);
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        }
+//        String filename = "/uploaded/" + fileName;
+        return file.getOriginalFilename();
     }
 
 }