月球大数据地理空间分析展示平台-【后端】-月球后台服务
13693261870
2023-11-28 4393d934ba300601a91084763616ea80379b550e
src/main/java/com/moon/server/helper/FileHelper.java
@@ -6,8 +6,11 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
@@ -445,4 +448,44 @@
            is.close();
        }
    }
    /**
     * 过滤输入字符串, 剔除多行注释以及替换掉反斜杠
     */
    public static String filter(String str) {
        return str.replaceAll("/\\*[\\s\\S]*?\\*/", "");
    }
    /**
     * 解码
     */
    public static String decode(String str) {
        try {
            return URLDecoder.decode(str, "UTF-8");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
    /**
     * 获取ClassPath-jar
     */
    public static String getClassPathForJar() {
        URL resource = ClassLoader.getSystemResource("");
        if (null == resource) {
            return null;
        }
        return decode(filter(resource.getPath()));
    }
    /**
     * 获取ClassPath-war
     */
    public static String getClassPathForWar() throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
        return context.getResource("").getFile().getAbsolutePath();
    }
}