Browse Source

Merge remote-tracking branch 'origin/master' into master_v2

jiequan.bi 4 năm trước cách đây
mục cha
commit
b79babdc72

+ 1 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/report/mapper/xml/KuaiShouYiCheReportMapper.xml

@@ -444,7 +444,7 @@
             and t1.stat_date = #{statDate}
         </if>
         ) t2
-        WHERE t2.campaignName in(581,582,583,584,552,553,554,680,681,845,846,847,848,849,850,1070,1071,1072,1066,1069,1064)
+        WHERE t2.campaignName in(68,441,469,470,481,581,582,583,584,552,553,554,680,681,845,846,847,848,849,850,1070,1071,1072,1066,1069,1063,1064)
 
     </select>
 

+ 21 - 117
module-shiwan/src/main/java/cn/com/ctop/shiwan/modules/FileUtil.java

@@ -16,13 +16,10 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
 import java.util.Date;
-import java.util.List;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
 /**
- * TODO
- *
  * @ClassName ZipUtil
  * @Author ZHAOXA
  * @date 2020-10-23 10:05
@@ -104,127 +101,25 @@ public class FileUtil {
                 os.write(buffer, 0, bytesRead);
                 os.flush();
             }
-            os.close();
-            is.close();
-        } catch (Exception e) {
-            log.error(" ========== 文件下载到本地异常:", e);
-            return null;
-        }
-        return realPath;
-    }
-
-
-    /**
-     * //创建zip方法,其中的参数ZipFileName是压缩后文件,inputFile要压缩的文件。
-     *
-     * @param
-     * @return void
-     * @throws
-     * @author ZHAOXA
-     */
-    public static void zip(String ZipFileName, File inputFile) throws IOException {
-        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(ZipFileName));//创建了输出流,这里ZipOutputStream()的参数是FileOutPutStream的原因是因为ZIP压缩的对象是文件。
-        zip(out, inputFile, "");//调用zip方法,实现了zip的重载。这次的参数是本方法中定义过的输出流out,要压缩的文件:inputFile以及文件最后的存储目录base
-        log.info("压缩中....");
-        out.close();
-        log.info("压缩完成!");
-    }
-
-    public static void zip(ZipOutputStream out, File f, String base) throws IOException {//方法重载
-        if (f.isDirectory()) {//判断要压缩的文件是否是文件夹
-            File[] fl = f.listFiles();//如果是文件夹就对把该文件夹中的文件的路径以文件数组的形式获得
-            if (base.length() != 0) {//判断最后要存储的路径是否已存在
-                out.putNextEntry(new ZipEntry(base + "/"));//如果已经存在,写入此目录的entry
-            }
-            for (int i = 0; i < fl.length; i++) {//遍历文件数组
-                zip(out, fl[i], base + fl[i]);//将文件数组中的文件添加到已经创建的ZIP中。
-            }
-        } else {
-            out.putNextEntry(new ZipEntry(base));//如果base为空。那么就创建新的进入点
-            //创建FileInputStream对象
-            FileInputStream in = new FileInputStream(f);//创建该输入流的原因是为了后边判断该流是否到达尾部
-            int b;
-            System.out.println(base);
-            while ((b = in.read()) != -1) {
-                out.write(b);//在没有到达尾部的情况下向已经创建的ZIP中添加
-            }
-            in.close();//关闭流
-        }
-    }
 
-    /**
-     * 文件压缩方法
-     *
-     * @param list
-     * @param zipfilename
-     * @param basePath
-     * @param vid
-     * @throws
-     * @Title: listToZip
-     * @date 2020年1月20日
-     */
-    public static String listToZip(List<String> list, String localPath, String zipfilename) {
-        long start = System.currentTimeMillis();
-        FileInputStream is = null;
-        ZipOutputStream zos = null;
-        String realPath = localPath + "/newFiles/" + zipfilename;
-        try {
-            if (list != null && list.size() > 0) {
-                File f = new File(localPath, "newFiles");
-                if (!f.exists()) {
-                    boolean mkdirFlag = f.mkdirs();
-                    if (!mkdirFlag) {
-                        log.error(" ==========压缩, 创建临时文件夹失败 ========== ");
-                        return null;
-                    }
-                }
-                // 创建zip文件输出流
-                zos = new ZipOutputStream(new FileOutputStream(new File(realPath)));
-                File file = null;
-                String path = "";
-                for (int i = 0; i < list.size(); i++) {
-                    path = list.get(i);
-                    // file 拿到待压缩文件
-                    file = new File(path);
-                    if (file.exists()) {
-                        // 创建源文件输入流
-                        is = new FileInputStream(file);
-                        zos.putNextEntry(new ZipEntry(file.getName()));
-                        byte[] buf = new byte[2048];
-                        int length = -1;
-                        while ((length = is.read(buf)) != -1) {
-                            zos.write(buf, 0, length);
-                            zos.flush();
-                        }
-                        zos.closeEntry();
-                        is.close();
-                    } else {
-                        log.warn("[ ======= {} 源文件不存在 ======= ]", path);
-                        return null;
-                    }
-                }
-            }
         } catch (Exception e) {
-            log.error("[ ==========压缩文件异常 ========== ]", e);
+            log.error(" ========== 文件下载到本地异常:", e);
             return null;
         } finally {
             try {
+                if (os != null) {
+                    os.close();
+                }
                 if (is != null) {
                     is.close();
                 }
-                if (zos != null) {
-                    zos.close();
-                }
             } catch (IOException e) {
-                log.error("[ ==========关闭IO流失败========== ]", e);
+                e.printStackTrace();
             }
         }
-        long end = System.currentTimeMillis();
-        log.info("文件压缩完成,耗时:{} ms", (end - start));
         return realPath;
     }
 
-
     /**
      * 通过MultipartFile获取本地下载路径
      *
@@ -278,24 +173,32 @@ public class FileUtil {
      * @Description
      * @author xukaixun
      */
-    public static String zipCompress(String zipPath, File sourceFile) {
+    public static String zipCompress(String zipSavePath, File sourceFile) {
         try {
             //创建zip输出流
-            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
+            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipSavePath));
             File[] fileList = sourceFile.listFiles();
-            if (fileList.length != 0) {
+            if (fileList.length != 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
+            {
                 for (File file : fileList) {
-                    //根据filename判断是否要最外层文件夹
-                    compress(zos, file, file.getName());
+                    compress(zos, file, sourceFile.getName() + "/" + file.getName());
                 }
                 zos.close();
             }
         } catch (Exception e) {
-            log.error("压缩文件异常: {}, zipSavePath={}, sourceFile={}", e, zipPath, sourceFile.getName());
+            log.error("zip compress file exception: {}, zipSavePath={}, sourceFile={}", e, zipSavePath, sourceFile.getName());
         }
-        return zipPath;
+        return zipSavePath;
     }
 
+    /**
+     * 递归压缩文件
+     *
+     * @param
+     * @return void
+     * @throws
+     * @author ZHAOXA
+     */
     private static void compress(ZipOutputStream zos, File sourceFile, String fileName) throws Exception {
         if (sourceFile.isDirectory()) {
             //如果是文件夹,取出文件夹中的文件(或子文件夹)
@@ -329,4 +232,5 @@ public class FileUtil {
         }
     }
 
+
 }

+ 3 - 0
module-shiwan/src/main/java/cn/com/ctop/shiwan/modules/mapper/xml/ShiwanFileUploadMapper.xml

@@ -23,6 +23,9 @@
         <if test="parentId != null and parentId != '' ">
             and parent_id = #{parentId}
         </if>
+        <if test="signature != null and signature != '' ">
+            and signature = #{signature}
+        </if>
     </select>
     <select id="selectListByParentId" resultType="cn.com.ctop.shiwan.modules.entity.ShiwanFileUpload">
         SELECT

+ 55 - 13
module-shiwan/src/main/java/cn/com/ctop/shiwan/modules/service/ShiwanFileUploadServiceImpl.java

@@ -51,10 +51,9 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
 
     @Override
     public ShiwanFileUpload saveOrUpdateFile(MultipartFile file, String userId, Long accountId, String fileType, Long parentId, String uuid) {
-        ShiwanFileUpload existFiles = getExistFiles(userId, accountId, file.getOriginalFilename(), fileType, parentId);
         StringBuffer path = new StringBuffer();
         path.append(downloadPath).append(DateUtils.getNowDate(DateUtils.WEB_FORMAT)).append("/").append(UUID.randomUUID().toString()).append("/");
-        log.info("----------下载到服务器地址:{}", path.toString());
+        log.info("-------下载到服务器地址:{}", path.toString());
         ShiwanFileUpload shiwanFileUpload = null;
         String newFileUrl = FileUtil.approvalFile(file, path.toString());
         String md5 = null;
@@ -63,25 +62,40 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
         } catch (IOException e) {
             log.error("获取MD5签名失败", e);
         }
+        ShiwanFileUpload existFiles = getExistFiles(userId, accountId, file.getOriginalFilename(), fileType, parentId, md5);
         if (!Check.isNull(existFiles)) {
+            long start = System.currentTimeMillis();
             existFiles.setSignature(md5);
-            shiwanFileUpload = updateFile(file, existFiles);
+            try {
+                shiwanFileUpload = updateFile(file, existFiles);
+            } catch (IOException e) {
+                log.error("更新文件,上传SOC异常", e);
+            }
             if (!Check.isNull(shiwanFileUpload)) {
                 List<String> urlList = unzipFiles(shiwanFileUpload, path.toString(), newFileUrl);
                 saveInnerFiles(shiwanFileUpload, urlList);
             }
+            long end = System.currentTimeMillis();
+            log.info("文件更新完成,耗时:{} ms", (end - start));
         } else {
+            long start = System.currentTimeMillis();
             ShiwanFileUpload fileUpload = new ShiwanFileUpload();
             fileUpload.setSignature(md5);
             fileUpload.setUserId(userId);
             fileUpload.setAccountId(accountId);
             fileUpload.setFileType(fileType);
-            fileUpload.setParentId(parentId);
-            shiwanFileUpload = saveFile(file, fileUpload, uuid);
+            try {
+                //新增zip包
+                shiwanFileUpload = saveFile(file, fileUpload, null);
+            } catch (IOException e) {
+                log.error("新增文件,上传SOC异常", e);
+            }
             if (!Check.isNull(shiwanFileUpload)) {
                 List<String> urlList = unzipFiles(shiwanFileUpload, path.toString(), newFileUrl);
                 saveInnerFiles(shiwanFileUpload, urlList);
             }
+            long end = System.currentTimeMillis();
+            log.info("文件新增完成,耗时:{} ms", (end - start));
         }
         return shiwanFileUpload;
     }
@@ -94,7 +108,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
      * @throws
      * @author ZHAOXA
      */
-    private ShiwanFileUpload updateFile(MultipartFile file, ShiwanFileUpload existFiles) {
+    private ShiwanFileUpload updateFile(MultipartFile file, ShiwanFileUpload existFiles) throws IOException {
         ResFileDTO resultFile = null;
         String url = "";
         ShiwanFileUpload fileUpload = new ShiwanFileUpload();
@@ -103,14 +117,20 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
         } else {
             url = "try-play/zip/";
         }
+        InputStream inputStream = null;
         try {
-            resultFile = CosUtils.uploadDetailInputStreamV2(file.getInputStream(), existFiles.getCfileName(), existFiles.getFileSuffix(), file.getSize(), url);
+            inputStream = file.getInputStream();
+            resultFile = CosUtils.uploadDetailInputStreamV2(inputStream, existFiles.getCfileName(), existFiles.getFileSuffix(), file.getSize(), url);
         } catch (IOException e) {
             log.error("更新文件,上传COS失败", e);
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
         }
         QueryWrapper<ShiwanFileUpload> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("id", existFiles.getId());
-        existFiles.setFileSize(resultFile.getFileSize() + "b");
+        existFiles.setFileSize(resultFile.getFileSize() + "B");
         existFiles.setUploadTime(new Date());
         shiwanFileUploadMapper.update(existFiles, queryWrapper);
         shiwanFileUploadMapper.deleteByParentId(existFiles.getId());
@@ -125,7 +145,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
      * @throws
      * @author ZHAOXA
      */
-    private ShiwanFileUpload saveFile(MultipartFile file, ShiwanFileUpload fileUpload, String uuid) {
+    private ShiwanFileUpload saveFile(MultipartFile file, ShiwanFileUpload fileUpload, String uuid) throws IOException {
         String filename = file.getOriginalFilename();
         ResFileDTO resultFile = null;
         String url = "";
@@ -136,10 +156,16 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
             url = "try-play/inner-file/".concat(uuid).concat("/");
         }
         String fileSuffix = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
+        InputStream inputStream = null;
         try {
-            resultFile = CosUtils.uploadDetailInputStream(file.getInputStream(), filename, fileSuffix, file.getSize(), url);
+            inputStream = file.getInputStream();
+            resultFile = CosUtils.uploadDetailInputStreamV2(inputStream, filename, fileSuffix, file.getSize(), url);
         } catch (IOException e) {
             log.error("上传COS异常", e);
+        } finally {
+            if (inputStream != null) {
+                inputStream.close();
+            }
         }
         if (!Check.isNull(resultFile)) {
             fileUpload.setCfileName(resultFile.getCFileName());
@@ -151,7 +177,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
                 fileUpload.setFileLevel(NoEn.NO1.valueInt());
             }
             fileUpload.setFileStatus(NoEn.NO1.valueStr());
-            fileUpload.setFileSize(resultFile.getFileSize() + "b");
+            fileUpload.setFileSize(resultFile.getFileSize() + "B");
             fileUpload.setFileUrl(resultFile.getFileUrl());
             fileUpload.setUploadTime(new Date());
             shiwanFileUploadMapper.insert(fileUpload);
@@ -206,6 +232,7 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
      * @author ZHAOXA
      */
     private List<String> unzipFiles(ShiwanFileUpload fileUpload, String path, String innerZipPath) {
+        long start = System.currentTimeMillis();
         List<String> urlList = new ArrayList<>();
         //下载到本地服务器
         if (Check.isNull(innerZipPath)) {
@@ -251,6 +278,8 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
             log.error("解压原zip文件失败 ", e);
         }
         deletelocalFiles(innerZipPath);
+        long end = System.currentTimeMillis();
+        log.info("文件解压完成,耗时:{} ms", (end - start));
         return urlList;
     }
 
@@ -298,13 +327,25 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
             shiwanFileUpload.setAccountId(newZipEntity.getAccountId());
             shiwanFileUpload.setFileType(newZipEntity.getFileType());
             shiwanFileUpload.setParentId(newZipEntity.getId());
+            String urlpath = url.substring(0, url.lastIndexOf("/"));
+            String lastFileName = urlpath.substring(urlpath.lastIndexOf("/") + 1);
+            String cosFilePath = "";
+            if ("innerFiles".equals(lastFileName)) {
+                cosFilePath = newZipEntity.getCfileName();
+            } else {
+                cosFilePath = newZipEntity.getCfileName().concat("/").concat(lastFileName);
+            }
             try {
                 shiwanFileUpload.setSignature(LoadFileUtil.getMD5(url));
             } catch (IOException e) {
                 log.error("获取MD5签名失败", e);
             }
             mulFileByPath = FileUtil.getMulFileByPath(url);
-            saveFile(mulFileByPath, shiwanFileUpload, newZipEntity.getCfileName());
+            try {
+                saveFile(mulFileByPath, shiwanFileUpload, cosFilePath);
+            } catch (IOException e) {
+                log.error("新增内部文件,上传SOC异常", e);
+            }
             deletelocalFiles(url);
         }
     }
@@ -332,13 +373,14 @@ public class ShiwanFileUploadServiceImpl extends ServiceImpl<ShiwanFileUploadMap
      * @throws
      * @author ZHAOXA
      */
-    private ShiwanFileUpload getExistFiles(String userId, Long accountId, String fileName, String fileType, Long parentId) {
+    private ShiwanFileUpload getExistFiles(String userId, Long accountId, String fileName, String fileType, Long parentId, String md5) {
         JSONObject job = new JSONObject();
         job.put("useId", userId);
         job.put("accountId", accountId);
         job.put("fileName", fileName);
         job.put("fileType", fileType);
         job.put("parentId", parentId);
+        job.put("signature", md5);
         ShiwanFileUpload fileEntity = shiwanFileUploadMapper.selectEntity(job);
         if (Check.isNull(fileEntity)) {
             return null;