java中各种路径获取方式

package com.example.demo.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @author :suyanlong
 * @date :Created in 2020/7/7 18:19
 * @description:获取项目路径工具类
 * @version: 1.0
 */
public class GetProjectPathUtils {

    /**
     * 得到工程的路径
     *
     * @return
     */
    public static String getProjectPath() {
        return System.getProperty("user.dir");
    }

    /**
     * 得到工程的路径
     * 尽量不要使用getProjectPath方法获取的路径,结果五花八门
     *
     * @return
     */
    public static String getSystemPath() {
        return ClassLoader.getSystemResource("").getPath();
    }

    /**
     * 获取标准的路径
     * 对于new File(".")和new File("..")
     * “."就表示当前的文件夹,而”..“则表示当前文件夹的上一级文件夹
     *
     * @param file
     * @return
     */
    public static String getCanonicalPath(File file) {
        try {
            return file.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 获取绝对路径
     * 对于new File(".")和new File("..")
     * 不管”.”、“..”,返回当前的路径加上你在new File()时设定的路径
     *
     * @param file
     * @return
     */
    public static String getAbsolutePath(File file) {
        try {
            return file.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 得到的只是你在new File()时设定的路径
     *
     * @param file
     * @return
     */
    public static String getPath(File file) {
        try {
            return file.getPath();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 类的绝对路径
     *
     * @return
     */
    public static String getClassPath() {
        try {
            return Class.class.getResource("/").getPath().substring(1);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 类的绝对路径
     *
     * @return
     */
    public static String getClassPath(Class clazz) {
        try {
            return clazz.getResource("").getPath();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 得到工程目录
     *
     * @return
     */
    public static String getRequestPath(HttpServletRequest request, String pacakgeName) {
        try {
            return request.getSession().getServletContext().getRealPath(pacakgeName);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 得到IE地址栏地址
     *
     * @return
     */
    public static String getRequestURL(HttpServletRequest request) {
        try {
            request.getServletPath();
            return request.getRequestURL().toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 得到相对地址
     *
     * @return
     */
    public static String getRequestURI(HttpServletRequest request) {
        try {
            request.getServletPath();
            return request.getRequestURI();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * System.getProperty()中的字符串参数值
     *
     * @return
     */
    public static void getPropertyValue() {
        System.out.println(System.getProperty("java.version "));
        System.out.println(System.getProperty("java.vendor  "));
        System.out.println(System.getProperty("java.vendor.url "));
        System.out.println(System.getProperty("java.home"));
        System.out.println(System.getProperty(" java.vm.specification.version"));
        System.out.println(System.getProperty("java.vm.specification.vendor "));
        System.out.println(System.getProperty("java.vm.specification.name"));
        System.out.println(System.getProperty("java.vm.version"));
        System.out.println(System.getProperty("java.vm.vendor"));
        System.out.println(System.getProperty("java.vm.name"));
        System.out.println(System.getProperty("java.specification.version"));
        System.out.println(System.getProperty("java.specification.vendor"));
        System.out.println(System.getProperty("java.specification.name"));
        System.out.println(System.getProperty("java.class.version"));
        System.out.println(System.getProperty("java.class.path"));
        System.out.println(System.getProperty("java.library.path"));
        System.out.println(System.getProperty("java.io.tmpdir"));
        System.out.println(System.getProperty("java.compiler "));
        System.out.println(System.getProperty("java.ext.dirs"));
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.arch"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("file.separator"));
        System.out.println(System.getProperty("path.separator"));
        System.out.println(System.getProperty("line.separator "));
        System.out.println(System.getProperty("user.name"));
        System.out.println(System.getProperty("user.home"));
        System.out.println(System.getProperty("user.dir"));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值