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"));
}
}