From f0ed6f16d980c2110ea508b3ef446b0adbfc2864 Mon Sep 17 00:00:00 2001
From: ChenJiaHe <763432473@qq.com>
Date: 星期三, 02 十二月 2020 17:41:54 +0800
Subject: [PATCH] 优化

---
 src/main/java/com/hx/util/FileUtils.java |   80 ++++++++++++++++++++++++++++++++++++++++
 src/main/java/com/hx/util/IPUtils.java   |   40 ++++++++++++++++++++
 2 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/util/FileUtils.java b/src/main/java/com/hx/util/FileUtils.java
index 0dc968c..5089158 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,57 @@
         }
     }
 
+    /**瑙嗛涓婁紶鐨勬柟娉�
+     * 淇濆瓨鍒版湇鍔″櫒閲岄潰鐨�
+     * @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 鍥剧墖鏂囦欢
@@ -339,4 +392,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;
+    }
+
+
 }
diff --git a/src/main/java/com/hx/util/IPUtils.java b/src/main/java/com/hx/util/IPUtils.java
new file mode 100644
index 0000000..b3e2b82
--- /dev/null
+++ b/src/main/java/com/hx/util/IPUtils.java
@@ -0,0 +1,40 @@
+package com.hx.util;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author ChenJiaHe
+ * @date 2020-12-2
+ */
+public class IPUtils {
+
+    /**鑾峰彇璁块棶ip鍦板潃*/
+    public static String getIpAddr(HttpServletRequest request) {
+        String ip=request.getHeader("x-forwarded-for");
+        if(null!=ip && ip.length()!=0 && !"unknown".equalsIgnoreCase(ip)) {
+            if(ip.indexOf(",")!=-1) {
+                ip=ip.split(",")[0];
+            }
+        }
+        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
+            ip=request.getHeader("Proxy-Client-Ip");
+        }
+        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
+            ip=request.getHeader("WL-Proxy-Client-IP");
+        }
+        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
+            ip=request.getHeader("HTTP_CLIENT_IP");
+        }
+        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
+            ip=request.getHeader("HTTP_X_FORWARDED_FOR");
+        }
+        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
+            ip=request.getHeader("X-Real-IP");
+        }
+        if(ip==null || ip.length()==0 || "unknown".equalsIgnoreCase(ip)) {
+            ip=request.getRemoteAddr();
+        }
+        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
+
+    }
+}

--
Gitblit v1.8.0