gdwebpath.java
来自「本书由浅入深、循序渐进地介绍了MVC的体系结构和如何构建一个基于MVC的Web框」· Java 代码 · 共 45 行
JAVA
45 行
package com.gd.mvc.util;
import java.io.IOException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
/**
* 这是公用类,用来获取Web环境的路径,名称等信息.
*/
public class GdWebPath {
private String webName;
private String webPhysicsPath;
private String systemName;
private String systemPath;
/**
* 该名称返回的是Web名称,如:myApp
* @return String
*/
public String getWebName(){
return this.webName;
}
/**
* 该路径指的是Web环境的物理路径,如:D:/eclipse/workspace/myApp
* @return String
*/
public String getWebPhysicsPath(){
return this.webPhysicsPath;
}
public void getPath() throws IOException {
Class cls2 = this.getClass();
ProtectionDomain pDomain = cls2.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
URL loc = cSource.getLocation();//file:/D:/eclipse/workspace/myApp/WEB-INF/classes/com/gd/web/WebPath.class
webPhysicsPath = loc.toString().substring(loc.toString().indexOf("/") + 1, loc.toString().indexOf("WEB-INF") - 1);
int num = webPhysicsPath.lastIndexOf("/");
webName = webPhysicsPath.substring(num + 1, webPhysicsPath.length());
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?