From 7a86d3b83f9154221f8bba17b159d56129d03264 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期五, 14 七月 2023 15:01:48 +0800
Subject: [PATCH] 拦截器优化

---
 src/main/java/com/hx/util/FileUtils.java |  346 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 335 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..7da1769 100644
--- a/src/main/java/com/hx/util/FileUtils.java
+++ b/src/main/java/com/hx/util/FileUtils.java
@@ -6,6 +6,9 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
+import java.net.FileNameMap;
+import java.net.URL;
+import java.net.URLConnection;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -218,6 +221,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 +367,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 +398,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 +509,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 +547,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 +600,67 @@
         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;
+    }
+
+    public static File httpUrlFile(String netUrl) throws IOException {
+        File file = File.createTempFile("temp123", ".xls");
+        InputStream inStream = null;
+        FileOutputStream os = null;
+
+        try {
+            file = File.createTempFile("net_url", ".jpg");
+            URL urlfile = new URL(netUrl);
+            inStream = urlfile.openStream();
+            os = new FileOutputStream(file);
+
+            byte[] buffer = new byte[8192];
+
+            int bytesRead;
+            while((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
+                os.write(buffer, 0, bytesRead);
+            }
+        } catch (Exception var15) {
+            var15.printStackTrace();
+        } finally {
+            try {
+                if (null != os) {
+                    os.close();
+                }
+
+                if (null != inStream) {
+                    inStream.close();
+                }
+            } catch (Exception var14) {
+                var14.printStackTrace();
+            }
+
+        }
+
+        return file;
+    }
+
 }

--
Gitblit v1.8.0