java获取项目名
A. java如何获取类的绝对路径
1 用servlet获取
1.1 获取项目的绝对路径
request.getSession().getServletContext().getRealPath("")
1.2 获取浏览器地址
request.getRequestURL()
1.3 获取当前文件的绝对路径
request.getSession().getServletContext().getRealPath(request.getRequestURI())
2.获取当前的classpath路径
String a2=类名.class.getResource("").toString();
String a3=DBConnection.class.getResource("/").toString();
String a4=DBConnection.class.getClassLoader().getResource("").toString();
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
//输出很好理解
3、获取文件的绝对路径
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"项目名\\WebContent\\文件";
B. JAVA中project name可以随便取嘛
JAVA中project name不能随便取。
有命名规范,项目名一般全小写。名称裂棚只能由字母、数字、下划线、$符号组成,且不能以数尘铅字开头,也不能使用JAVA中的关键字。
另外,项目名一般取有意义的名肆兄则字,和项目有关联。除此之外,可以自由取名。
C. Java web项目,在.java程序中如何获取webapp路径
String t=Thread.currentThread().getContextClassLoader().getResource("链哗").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"项目名\\WebContent\散唤档\文件";
复制,亲测有效。冲乱
D. 请问在java代码中如何获得web项目名
项目名:
String path = request.getContextPath();
WEB路含纯祥径:
String basePath = request.getScheme()+"://"+request.getServerName()+":"裤如+request.getServerPort()+path+"/谈搏";
E. java获取请求域名
很多朋友都想知道java如何获取请求域名?下面就一起来了解一下吧~
1、获取协议名和域名。
request.getScheme(); //得到协议首此世名 例如:http request.getServerName(); //得到域名 localhost
2、获取全路径。
request.getRequestURL(); //得到http://localhost:8888/CRM/loginController/login
3、获取请求所有参数 //map类型。
request.getParameterMap()
4、获取项目名
request.getContextPath(); 者肢// /CRM
5、获取请求方法
request.getServletPath(); // /loginController/login
/** * 获取当前访问URL (含协议、域名、端口号[忽略80端口]、项目名) * @param request * @return: String */ public static String getServerUrl(HttpServletRequest request) { // 访问协议 String agreement = request.getScheme(); // 访问域名 String serverName = request.getServerName(); // 访问端口号 int port = request.getServerPort(); // 访问项目名 String contextPath = request.getContextPath(); String url = "%s://%s%s%s"; 扒咐 String portStr = ""; if (port != 80) { portStr += ":" + port; } return String.format(url, agreement, serverName, portStr, contextPath); }
F. 在java类中怎么获得java项目的目录
一 相对路径的获得
说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目)
String relativelyPath=System.getProperty("user.dir");
上述相对路径中,java项目中的文件是相对于项目的根目录
web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于 tomcat安装目录\bin)
二 类加载目录的获得(即当运行时某一类时获得其装载目录)
1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录)
InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
(test.txt文件的路径为 项目名\src\test.txt;类TestAction所在包的第一级目录位于src目录下)
上式中将TestAction,test.txt替换成对应成相应的类名和文件名字即可
1.2)通用方法二 (此方法和1.1中的方法类似,不同的是此方法必须以'/'开头,参考http://riddickbryant.iteye.com/blog/436693)
InputStream is=Test1.class.getResourceAsStream("/test.txt");
(test.txt文件的路径为 项目名\src\test.txt,类Test1所在包的第一级目录位于src目录下)
三 web项目根目录的获得(发布之后)
1 从servlet出发
可建立一个servlet在其的init方法中写入如下语句
ServletContext s1=this.getServletContext();
String temp=s1.getRealPath("/"); (关键)
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\ (002_ext为项目名字)
如果是调用了s1.getRealPath("")则输出D:\工具\Tomcat-6.0\webapps\002_ext(少了一个"\")
2 从httpServletRequest出发
String cp11111=request.getSession().getServletContext().getRealPath("/");
结果形如:D:\工具\Tomcat-6.0\webapps\002_ext\
四 classpath的获取(在Eclipse中为获得src或者classes目录的路径)
方法一 Thread.currentThread().getContextClassLoader().getResource("").getPath()
eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("t---"+t);
输出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/
方法二 JdomParse.class.getClassLoader().getResource("").getPath() (JdomParse为src某一个包中的类,下同)
eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);
输出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/
另外,如果想把文件放在某一包中,则可以 通过以下方式获得到文件(先定位到该包的最后一级目录)
eg String p2=JdomParse.class.getResource("").getPath();
System.out.println("JdomParse.class.getResource---"+p2);
输出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse为src目录下jdom包中的类)
四 属性文件的读取:
方法 一
InputStream in = lnew BufferedInputStream( new FileInputStream(name)); Properties p = new Properties(); p.load(in);
注意路径的问题,做执行之后就可以调用p.getProperty("name")得到对应属性的值
方法二
Locale locale = Locale.getDefault();
ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale);
String value = localResource.getString("test");
System.out.println("ResourceBundle: " + value);
工程src目录下propertiesTest.properties(名字后缀必须为properties)文件内容如下:
test=hello word
G. 普元EOS用java怎么获取项目的路径的吗
EOS项目中获取当前构消亩件包配置目录下文件路径import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.eos.foundation.eoscommon.CommonUtil;
public abstract class {
public String (String configurationFileName) {
String packageName = CommonUtil.getCurrentContributionName();
final String FORMAT_STR = ".xml";
if(configurationFileName == null || "".equals(configurationFileName)) {
throw new IllegalArgumentException("构件包:"卜桥镇 + packageName + " 的配置文件名不能为空!");
} else if(configurationFileName.length() < 5 ||
!FORMAT_STR.equals(configurationFileName.substring(configurationFileName.length() - 4, configurationFileName.length()))) {
throw new IllegalArgumentException("配置文件必须是XML格式的文件!");
}
String separator = File.separator;
String classesPath = .class.getClassLoader().getResource("").toString();
String classerDealPath = classesPath.substring(0, classesPath.length()-1);
String webInfPath = classerDealPath.substring(6, classerDealPath.lastIndexOf("/型粗"));
String packagePath = webInfPath + separator + "_srv"+separator+"work"+separator+"user" +separator+ packageName;
String configFileAbsolutePath = packagePath + separator + "META-INF" + separator + configurationFileName;
return configFileAbsolutePath;
}
protected FileInputStream (String configurationAbsolutePath) throws FileNotFoundException {
File file = new File(configurationAbsolutePath);
return new FileInputStream(file);
}
}
H. java windows和linux获取项目根目录的方法一致吗
java有个特性是跨平台性,所以其获取项目根目录的方法是一样的。
request.getContextPath()方法就是是得到项目的名字,如果项目为根目录,则得到一个"",即空的字条串。如果项目为abc,<%=request.getContextPath()%> 将得到abc,服务器端的路径则会自动加上,<a href="XXXX.jsp"> 是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base来解决路径的问题,不多用的最多的还是request.getContextPath。
I. java request 如何取到发送请求的地址是什么
request对象通过以下方法来获取请求路径,如下所示:
String getServerName():获取服务器名,localhost;
String getServerPort():获取服务器端口号,8080;
String getContextPath():获取项目名,/Example;
String getServletPath():碰举获取Servlet路誉态径,/AServlet;
String getQueryString():获取参数部分,即问号后面的部笑虚碧分:username=zhangsan
String getRequestURI():获取请求URI,等于项目名+Servlet路径:/Example/AServlet
String getRequestURL():获取请求URL,等于不包含参数的整个请求路径:http://localhost:8080/Example/AServlet 。
J. 通过java获取当前项目路径
getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)
例如 项目在/D:/workspace/MainStream/Test
在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/
publicStringgetCurrentPath(){
//取得根目录路径
StringrootPath=getClass().getResource("/").getFile().toString();
//当前目录路径
StringcurrentPath1=getClass().getResource(".").getFile().toString();
StringcurrentPath2=getClass().getResource("").getFile().toString();
//当前目录的上级目录路径
StringparentPath=getClass().getResource("../").getFile().toString();
returnrootPath;
}