package com.hibegin.http.server.util;

import com.hibegin.common.util.IOUtil;
import com.hibegin.common.util.LoggerUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.logging.Level;

/**
 * 提供给一些路径供程序更方便的调用
 */
public class PathUtil {

    private static String ROOT_PATH = "";

    public static String getConfPath() {
        return getRootPath() + "/conf/";
    }

    public static String getRootPath() {
        if (ROOT_PATH != null && ROOT_PATH.length() > 0) {
            return ROOT_PATH;
        } else {
            String path;
            if (PathUtil.class.getResource("/") != null) {
                URL url = PathUtil.class.getResource("/");
                String tPath = url.getPath().replace("file:", "");
                try {
                    path = URLDecoder.decode(tPath, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    //e.printStackTrace();
                    path = tPath;
                }
                path = new File(path).getParent();
            } else {
                path = System.getProperty("user.dir");
            }
            return path;
        }
    }

    public static void setRootPath(String rootPath) {
        ROOT_PATH = rootPath;
    }

    public static File getConfFile(String file) {
        File nFile = new File(getConfPath() + file);
        if (nFile.exists()) {
            return nFile;
        } else {
            InputStream in = PathUtil.class.getResourceAsStream("/conf/" + file);
            if (in != null) {
                try {
                    File tempFile = File.createTempFile(nFile.getName(), ".tmp");
                    IOUtil.writeBytesToFile(IOUtil.getByteByInputStream(in), tempFile);
                    return tempFile;
                } catch (IOException e) {
                    LoggerUtil.getLogger(PathUtil.class).log(Level.SEVERE, "", e);
                }
            }
        }
        return null;
    }

    public static String getStaticPath() {
        return getRootPath() + "/static/";
    }

    public static String getTempPath() {
        String str = getRootPath() + "/temp/";
        new File(str).mkdirs();
        return str;
    }
}
@@ -196,102 +196,8 @@
 am;%0A
-import java.io.UnsupportedEncodingException;%0Aimport java.net.URL;%0Aimport java.net.URLDecoder;%0A
 impo
@@ -224,16 +224,16 @@
 .Level;%0A
+
 %0A/**%0A * 
@@ -573,294 +573,71 @@
     
-S
 tr
-ing path;%0A            if (PathUtil.class.getResource(%22/%22) != null) %7B%0A                URL url = PathUtil.class.getResource(%22/%22);%0A                String tPath = url.getPath().replace(%22file:%22, %22%22);%0A                try %7B%0A                    path = URLDecoder.decode(tPath, %22UTF-8%22);%0A    
+y %7B%0A                return new File(%22.%22).getCanonicalPath();%0A
     
@@ -657,27 +657,10 @@
 ch (
-UnsupportedEncoding
+IO
 Exce
@@ -690,252 +690,52 @@
     
-    //e.printStackTrace();%0A                    path = tPath;%0A                %7D%0A                path = new File(path).getParent();%0A            %7D else %7B%0A                path = System.getProperty(%22user.dir%22);%0A            %7D%0A            return path;
+throw new RuntimeException(e);%0A            %7D
 %0A   

Fix missing close ?