/**
* 获取项目路径
* @return null 或项目路径
* @throws IOException
*/
public static String getPojectPath() {
File directory = new File("");//参数为空
try {
return directory.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取类的包路径
* @return
*/
public static String getModelSrc(Class c,String name) {
Package p = c.getPackage();
String pkg=p.getName();
int index = pkg.lastIndexOf(".");
return pkg=pkg.substring(0, index)+"."+name;
}
/**
* 获取项目域名
* @param req
* @return
*/
public static String projectURL(HttpServletRequest req) {
return req.getScheme() //当前链接使用的协议
+"://" + req.getServerName()//服务器地址
+ ":" + req.getServerPort() //端口号
+ req.getContextPath(); //应用名称,如果应用名称为
// + req.getServletPath() ;//请求的相对url
}
/**
*
* projectURLs(Use a phrase to describe here the role of this method)
* (This method applicable conditions described here - optional)
* @param req
* @return [Scheme ,getServerName,getServerPort,getContextPath,getServletPath]
*Object[]
* @exception
* @since 1.0.0
*/
public static Object[] projectURLs(HttpServletRequest req) {
return new Object[] {req.getScheme(), //当前链接使用的协议
req.getServerName(),//服务器地址
req.getServerPort(), //端口号
req.getContextPath(),//应用名称,如果应用名称为
req.getServletPath() };//请求的相对url
}
/**
* 获取项目域名
* @param req
* @return
*/
public static String projectURLall(HttpServletRequest req) {
return req.getScheme() //当前链接使用的协议
+"://" + req.getServerName()//服务器地址
+ ":" + req.getServerPort()//端口号
+ "/"+req.getContextPath(); //应用名称,如果应用名称为
// + req.getServletPath() ;//请求的相对url
}
public static String projectURLNOScheme(HttpServletRequest req) {
return req.getServerName()//服务器地址
+ ":" + req.getServerPort(); //端口号
// + req.getContextPath(); //应用名称,如果应用名称为
// + req.getServletPath() ;//请求的相对url
}
/**
* file:/D:/server/work/proname/build/classes/
* @return
*/
public static String buildPath() {
URL uri = ProJectUtils.class.getClassLoader().getResource("");
return uri.getPath();
}
public static void main(String[] args) {
System.out.println(JSON.toJSONString(getPojectPath()));
}
public static String classpath() {
return Thread.currentThread().getContextClassLoader().getResource("").getPath();
}
标签:req,return,String,路径,classpath,static,java,public,getServerPort
From: https://blog.csdn.net/a913222/article/details/137298449