获取绝对路径 去除路径的% 空格问题

class 类: javascript

public static String getBasePath() {

		String savaPath = "";

		savaPath = 类.class.getRecource("").getPath();

		int i = savaPath.lastIndexOf("WEB-INF");

		savaPath = savaPath.substring(1, i);

		return savaPath;

	}

这个是转换: java

String  savePath =  java.net.URLDecoder.decode(savaPath,"utf-8");

     java 路径解决方案Java的路径问题,相对来讲就比较繁杂。最近的工做涉及到建立和读取文件的工做,现将实际使用中遇到的问题总结以下:
    一 相对路径的解释
    1.相对路径(即相对于当前用户目录的相对路径)都可经过如下方式得到(不管是通常的java项目仍是web项目) web

String relativelyPath=System.getProperty("user.dir");

    对于通常的java项 目中的文件是相对于项目的根目录,而对于web项目中的文件路径,多是服务器的某个路径,同时不一样的web服务器也不一样(tomcat是相对于 tomcat安装目录\bin)。为此,我的认为,在web项目中,最好不要使用“相对于当前用户目录的相对路径”。然而默认状况下,java.io 包中的类老是根据当前用户目录来分析相对路径名。此目录由系统属性 user.dir 指定,一般是 Java 虚拟机的调用目录。这就是说,在使用java.io包中的类时,最好不要使用相对路径。不然,虽然在SE程序中可能还算正常,可是到了EE程序中,弄很差,就会带来问题一片哦。  tomcat


    2.相对于classpath的相对路径
    如:相对于file:/D:/mywork/javaprj/MyTest/bin这个路径的相对路径。其中,bin是本项目的classpath。全部的Java源文件编译后的.class文件复制到这个目录中。 服务器

    二 类加载目录(即当运行某一类时得到其装载目录)
    1.不管是通常的java项目仍是web项目,先定位到能看到包路径的第一级目录    app

InputStream is=ReadWrite.class.getClassLoader().getResourceAsStream("DeviceNO");

其中,DeviceNO文件的路径为 项目名\src\DeviceNO;类ReadWrite所在包的第一级目录位于src目录下。 webapp


    2.与1类似,不一样的是此方法必须以'/'开头       函数

InputStream is=ReadWrite.class.getResourceAsStream("DeviceNO");

其中,DeviceNO文件的路径为 项目名\src\DeviceNO;类ReadWrite所在包的第一级目录位于src目录下。 this



    三. web项目根目录获取
    1. 可创建一个servlet,在其init方法中写入以下语句 编码

ServletContext sc=this.getServletContext(); 
String temp=sc.getRealPath("/");

获得的输出路径结果相似:"D:\Apache\Tomcat6.0\webapps\windpower\ " (windpower为项目名字) ,若是是调用了s1.getRealPath("")则输出"D:\Apache\Tomcat6.0\webapps\windpower"(注意,在 最后少了一个"\")

    

    2. 在httpServletRequest中,能够经过下面语句
String cp=request.getSession().getServletContext().getRealPath("/"); 获得的输出路径结果相似:"D:\Apache\Tomcat6.0\webapps\windpower\ "



    四 .类路径( classpath)的获取(在Eclipse/MyEclipse中,则为得到src或者classes目录的路径)

    方法1.  Thread.currentThread().getContextClassLoader().getResource("").getPath()
    例如:

String path=Thread.currentThread().getContextClassLoader().getResource("").getPath(); 
System.out.println(path);

打印:“/D:/windpower/WebRoot/WEB-INF/classes/” 
  方法2.   ParsingXML.class.getClassLoader().getResource("").getPath()(ParsingXML为src某一个包中的类,下同)
    例如:

String path=ParsingXML.class.getClassLoader().getResource("").getPath(); 
System.out.println("ParsingXML.class.getClassLoader().getResource--"+path);

打印: “ParsingXML.class.getClassLoader().getResource--/D:/windpower/WebRoot/WEB-INF/classes/”

    另外,若是想把文件放在某一包中,则能够经过如下方式得到到文件所在目录,即先定位到该包的最后一级目录。

ParsingXML.class.getResource("").getPath();

例如:

String path=ParsingXML.class.getResource("").getPath(); 
System.out.println("ParsingXML.class.getResource---"+p2);

打印: “ParsingXML.class.getResource---/D:/windpower/WebRoot/WEB-INF/classes/parsing/ ”(ParsingXML为src目录下parsing包中的类)

    五. 属性文件的读取:

    方法1.

static {
		ps = new Properties(); 
		try { 
			InputStream in = ReadWrite.class.getResourceAsStream("DeviceNO"); 
			ps.load(in); 
			in.close(); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
		ps.getProperty("key") ;
	}


    方法2.

Locale locale = Locale.getDefault();  
ResourceBundle localResource = ResourceBundle.getBundle("windpower/DeviceNOProperties", locale);  
String value = localResource.getString("1");  
System.out.println("DeviceNO: " + value);

工程src目录下文件DeviceNOProperties.properties(名字后缀必须为properties)文件内容以下:1=3输出结果为:“DeviceNO:3”

    六.编码转换问题:
    ClassLoader的getResource方法使用了utf-8对路径信息进行了编码,当路径中存在中文和空格时,他会对这些字符进行转换,这样, 获得的每每不是咱们想要的真实路径,在此,调用了URLDecoder的decode方法进行解码,以便获得原始的中文及空格路径

    例如:结果是file:/C:/Documents%20and%20Settings/%e5%ba%84%e6%99%93%e6%af%85  
/Local%20Settings/Temp/temp0.jar!/db/dmozdata.mdb  
    而咱们指望是 C:/Documents 路径p source  等等。这里咱们只要在获取到路径以前把返回值decode下就能够了. 用utf-8编码. Java代码 :

String configPath = this.getClass().getClassLoader().getResource("allowPath.xml").getFile(); 
configPath = java.net.URLDecoder.decode(configPath,"utf-8");

另外java中URL 的编码和解码函数java.net.URLEncoder.encode(String s)和java.net.URLDecoder.decode(String s);在javascript 中URL 的编码和解码函数escape(String s)和unescape(String s) ;  

    七.总结:
    咱们在使用相对路径时,应当使用相对于当前classpath的相对路径。
    ClassLoader类的getResource(String name),getResourceAsStream(String name)等方法,使用相对于当前项目的classpath的相对路径来查找资源。
    读取属性文件经常使用到的ResourceBundle类的getBundle(String path)也是如此。
经过查看ClassLoader类及其相关类的源代码,发现它实际上仍是使用了URI形式的绝对路径。经过获得当前classpath的URI形式的绝对路径,再去构建相对路径的URI形式的绝对路径。



    本身遇到的问题:
    因为安装的tomcat在安装后,生成的名称为Tomcat 6.0,就是中间有个空格,因此在查找web项目的路径时,就报错。

private String getBasePath() throws IOException {
		ClassPathResource resource = new ClassPathResource("../../");
		return resource.getURL().getPath();
	}
后来发现,是在去路径时,把空格读成“%20”了,就把上面的方法里返回一句添加了替换: return resource.getURL().getPath().replace("%20", " "); 就解决了问题。
相关文章
相关标签/搜索