ChenJiaHe
2020-12-02 f0ed6f16d980c2110ea508b3ef446b0adbfc2864
优化
1个文件已添加
1个文件已修改
120 ■■■■■ 已修改文件
src/main/java/com/hx/util/FileUtils.java 80 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/util/IPUtils.java 40 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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指向的前端文件夹(统一文件夹),如:user/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);
        //是视频type是为空的
        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;
    }
}
src/main/java/com/hx/util/IPUtils.java
New file
@@ -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;
    }
}