From 8ab2ad5580212b91df848e4c127f2a682485fde3 Mon Sep 17 00:00:00 2001
From: zhouxiang <zhouxiang>
Date: 星期一, 25 四月 2022 01:49:15 +0800
Subject: [PATCH] 修改common

---
 src/main/java/com/hx/util/FileUtils.java |  309 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 298 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/hx/util/FileUtils.java b/src/main/java/com/hx/util/FileUtils.java
index 0dc968c..6911a87 100644
--- a/src/main/java/com/hx/util/FileUtils.java
+++ b/src/main/java/com/hx/util/FileUtils.java
@@ -6,6 +6,8 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
+import java.net.FileNameMap;
+import java.net.URLConnection;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -218,6 +220,131 @@
         }
     }
 
+    /**
+     *  2020-06-29
+     *  cjh
+     * 鍥剧墖鏍煎紡鍒ゆ柇
+     * */
+    public static boolean imageFormatJudge(File firs) {
+        String imageName = firs.getName();
+        //鎴彇鏍煎紡
+        String suffix =imageName.substring(imageName.lastIndexOf(".") + 1);
+        //鏍煎紡瀛楁瘝杞皬鍐�
+        suffix = suffix.toLowerCase();
+        //杩涜鍒ゆ柇
+        if(suffix.equals("png")) {
+            return true;
+        }else if(suffix.equals("jpg")){
+            return true;
+        }else if(suffix.equals("jpeg")){
+            return true;
+        }else {
+            return false;
+        }
+    }
+
+    /**瑙嗛涓婁紶鐨勬柟娉�
+     * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨�
+     * @param platformIconFile 瑙嗛鏂囦欢
+     * @param unifiedFolder NG鎸囧悜鐨勫墠绔枃浠跺す(缁熶竴鏂囦欢澶�)锛屽锛歶ser/local/images/
+     * @param saveFolder 淇濆瓨鍒扮殑鏂囦欢澶�,濡傦細/bananer/
+     * @param autoDateFolder 鏄惁鐢熸垚鏃ユ湡鏂囦欢澶�
+     * @return 鍥剧墖璺緞
+     * 2020-06-29 ChenJiaHe
+     */
+    public static String videoFileUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder
+            ,boolean autoDateFolder) {
+        String fileName = "";
+        try {
+            if(platformIconFile == null) {
+                throw new TipsException("璇蜂笂浼犺棰戞枃浠讹紒");
+            }
+            if(!getMimeType(platformIconFile.getOriginalFilename())){
+                throw new TipsException("璇蜂笂浼犺棰戞牸寮忕殑鏂囦欢锛�");
+            }
+
+            //璁剧疆鍥剧墖澶у皬
+            // String.format("%.1f",platformIconFile.getSize()/1024.0);
+            if(autoDateFolder){
+                if(saveFolder.endsWith("/")){
+                    saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/";
+                }else{
+                    saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/";
+                }
+            }
+            fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS");
+            if(unifiedFolder.endsWith("/")){
+                if(saveFolder.startsWith("/")){
+                    saveFolder = saveFolder.replaceFirst("/","");
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+saveFolder;
+                }
+            }else{
+                if(saveFolder.startsWith("/")){
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+"/"+saveFolder;
+                }
+            }
+            fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName);
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+        }
+        return fileName;
+    }
+
+    /**鍥剧墖涓婁紶鐨勬柟娉�
+     * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨�
+     * @param platformIconFile 鍥剧墖鏂囦欢
+     * @param unifiedFolder NG鎸囧悜鐨勫墠绔枃浠跺す(缁熶竴鏂囦欢澶�)锛屽锛歶ser/local/images/
+     * @param saveFolder 淇濆瓨鍒扮殑鏂囦欢澶�,濡傦細/bananer/
+     * @param autoDateFolder 鏄惁鐢熸垚鏃ユ湡鏂囦欢澶�
+     * @return 鍥剧墖璺緞
+     * 2020-06-29 ChenJiaHe
+     */
+    public static String handleFileUpload(File platformIconFile,String unifiedFolder,String saveFolder
+            ,boolean autoDateFolder) {
+        String fileName = "";
+        try {
+            if(platformIconFile == null) {
+                throw new TipsException("璇蜂笂浼犲浘鐗囷紒");
+            }
+            if(!imageFormatJudge(platformIconFile)) {
+                throw new TipsException("璇蜂笂浼爌ng銆乯pg鍜宩peg鏍煎紡鐨勫浘鐗囷紒");
+            }
+
+            //璁剧疆鍥剧墖澶у皬
+            // String.format("%.1f",platformIconFile.getSize()/1024.0);
+            if(autoDateFolder){
+                if(saveFolder.endsWith("/")){
+                    saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/";
+                }else{
+                    saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/";
+                }
+            }
+            fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS");
+            if(unifiedFolder.endsWith("/")){
+                if(saveFolder.startsWith("/")){
+                    saveFolder = saveFolder.replaceFirst("/","");
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+saveFolder;
+                }
+            }else{
+                if(saveFolder.startsWith("/")){
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+"/"+saveFolder;
+                }
+            }
+            fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName);
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+        }
+        return fileName;
+    }
+
     /**鍥剧墖涓婁紶鐨勬柟娉�
      * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨�
      * @param platformIconFile 鍥剧墖鏂囦欢
@@ -239,7 +366,7 @@
             }
 
             //璁剧疆鍥剧墖澶у皬
-           // String.format("%.1f",platformIconFile.getSize()/1024.0);
+            // String.format("%.1f",platformIconFile.getSize()/1024.0);
             if(autoDateFolder){
                 if(saveFolder.endsWith("/")){
                     saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/";
@@ -270,12 +397,104 @@
     }
 
     /**
+     * 闊抽涓婁紶
+     * @param platformIconFile
+     * @param unifiedFolder
+     * @param saveFolder
+     * @param autoDateFolder
+     * @return
+     */
+    public static String handleAudioUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder
+            ,boolean autoDateFolder) {
+        String fileName = "";
+        try {
+            if(platformIconFile == null) {
+                throw new TipsException("璇蜂笂浼犻煶棰戯紒");
+            }
+
+            if(autoDateFolder){
+                if(saveFolder.endsWith("/")){
+                    saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/";
+                }else{
+                    saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/";
+                }
+            }
+
+            fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS");
+            if(unifiedFolder.endsWith("/")){
+                if(saveFolder.startsWith("/")){
+                    saveFolder = saveFolder.replaceFirst("/","");
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+saveFolder;
+                }
+            }else{
+                if(saveFolder.startsWith("/")){
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+"/"+saveFolder;
+                }
+            }
+            fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName);
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+        }
+        return fileName;
+    }
+
+    /**
+     * 鏂囦欢涓婁紶
+     * @param platformIconFile
+     * @param unifiedFolder
+     * @param saveFolder
+     * @param autoDateFolder
+     * @return
+     */
+    public static String handleOtherFileUpload(MultipartFile platformIconFile,String unifiedFolder,String saveFolder
+            ,boolean autoDateFolder) {
+        String fileName = "";
+        try {
+            if(platformIconFile == null) {
+                throw new TipsException("璇蜂笂浼犳枃浠讹紒");
+            }
+
+            if(autoDateFolder){
+                if(saveFolder.endsWith("/")){
+                    saveFolder = saveFolder+dateFormat(new Date(),"yyyyMM")+"/";
+                }else{
+                    saveFolder = saveFolder+"/"+dateFormat(new Date(),"yyyyMM")+"/";
+                }
+            }
+
+            fileName = dateFormat(new Date(),"yyyyMMddHHmmssSSS");
+            if(unifiedFolder.endsWith("/")){
+                if(saveFolder.startsWith("/")){
+                    saveFolder = saveFolder.replaceFirst("/","");
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+saveFolder;
+                }
+            }else{
+                if(saveFolder.startsWith("/")){
+                    unifiedFolder  = unifiedFolder + saveFolder;
+                }else{
+                    unifiedFolder  = unifiedFolder+"/"+saveFolder;
+                }
+            }
+            fileName = saveFolder+fileUp(platformIconFile,unifiedFolder,fileName);
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+        }
+        return fileName;
+    }
+
+    /**
      * 2020-06-29 ChenJiaHe
-聽聽 聽 * @param file 聽聽 聽聽聽 聽聽聽 聽//鏂囦欢瀵硅薄
-聽聽 聽 * @param filePath聽聽 聽聽聽 聽//涓婁紶璺緞
-聽聽 聽 * @param fileName聽聽 聽聽聽 聽//鏂囦欢鍚�
-聽聽 聽 * @return聽 鏂囦欢鍚�
-聽聽 聽 */
+     聽聽 聽 * @param file 聽聽 聽聽聽 聽聽聽 聽//鏂囦欢瀵硅薄
+     聽聽 聽 * @param filePath聽聽 聽聽聽 聽//涓婁紶璺緞
+     聽聽 聽 * @param fileName聽聽 聽聽聽 聽//鏂囦欢鍚�
+     聽聽 聽 * @return聽 鏂囦欢鍚�
+     聽聽 聽 */
     public static String fileUp(MultipartFile file, String filePath, String fileName){
         String extName = ""; // 鎵╁睍鍚嶆牸寮忥細
         try {
@@ -289,13 +508,34 @@
         return fileName+extName;
     }
 
+
     /**
-     聽聽 聽 * 鍐欐枃浠跺埌褰撳墠鐩綍鐨剈pload鐩綍涓�
-     聽聽 聽 *
-     聽聽 聽 * @param in
-     聽聽 聽 * @param fileName
-     聽聽 聽 * @throws IOException
+     * 2020-06-29 ChenJiaHe
+     聽聽 聽 * @param file 聽聽 聽聽聽 聽聽聽 聽//鏂囦欢瀵硅薄
+     聽聽 聽 * @param filePath聽聽 聽聽聽 聽//涓婁紶璺緞
+     聽聽 聽 * @param fileName聽聽 聽聽聽 聽//鏂囦欢鍚�
+     聽聽 聽 * @return聽 鏂囦欢鍚�
      聽聽 聽 */
+    public static String fileUp(File file, String filePath, String fileName){
+        String extName = ""; // 鎵╁睍鍚嶆牸寮忥細
+        try {
+            if (file.getName().lastIndexOf(".") >= 0){
+                extName = file.getName().substring(file.getName().lastIndexOf("."));
+            }
+            copyFile(file, filePath, fileName+extName).replaceAll("-", "");
+        } catch (IOException e) {
+            System.out.println(e);
+        }
+        return fileName+extName;
+    }
+
+    /**
+ 聽聽 聽 * 鍐欐枃浠跺埌褰撳墠鐩綍鐨剈pload鐩綍涓�
+ 聽聽 聽 *
+ 聽聽 聽 * @param in
+ 聽聽 聽 * @param fileName
+ 聽聽 聽 * @throws IOException
+ 聽聽 聽 */
     private static String copyFile(InputStream in, String dir, String realName)throws IOException {
         File file = new File(dir, realName);
         file.setWritable(true);
@@ -306,6 +546,26 @@
             file.createNewFile();
         }
         org.apache.commons.io.FileUtils.copyInputStreamToFile(in, file);
+        return realName;
+    }
+
+    /**
+     聽聽 聽 * 鍐欐枃浠跺埌褰撳墠鐩綍鐨剈pload鐩綍涓�
+     聽聽 聽 *
+     聽聽 聽 * @param in
+     聽聽 聽 * @param fileName
+     聽聽 聽 * @throws IOException
+     聽聽 聽 */
+    private static String copyFile(File fileIn, String dir, String realName)throws IOException {
+        File file = new File(dir, realName);
+        file.setWritable(true);
+        if (!file.exists()) {
+            if (!file.getParentFile().exists()) {
+                file.getParentFile().mkdirs();
+            }
+            file.createNewFile();
+        }
+        org.apache.commons.io.FileUtils.copyFile(fileIn,file);
         return realName;
     }
 
@@ -339,4 +599,31 @@
         return targetFile;
     }
 
+    /**鍒ゆ柇鏄笉鏄棰戞枃浠�
+     * @param fileName 鏂囦欢鍚嶇О
+     * @return boolean true鏄棰戞枃浠�
+     */
+    public static boolean getMimeType(String fileName) {
+        boolean b = false;
+        FileNameMap fileNameMap = URLConnection.getFileNameMap();
+        String type = fileNameMap.getContentTypeFor(fileName);
+        //鏄棰憈ype鏄负绌虹殑
+        if(StringUtils.isEmpty(type)) {
+            b = true;
+        }
+        return b;
+    }
+
+    /**
+     * 鍒ゆ柇鏄惁涓鸿棰�
+     * @param fileName
+     * @return
+     */
+    public static String isVideo(String fileName) {
+        FileNameMap fileNameMap = URLConnection.getFileNameMap();
+        String type = fileNameMap.getContentTypeFor(fileName);
+        return type;
+    }
+
+
 }

--
Gitblit v1.8.0