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

class 类:

public static String getBasePath() {

		String savaPath = "";

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

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

		savaPath = savaPath.substring(1, i);

		return savaPath;

	}

这个是转换:

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

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

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

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


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

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

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

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


    2.与1相似,不同的是此方法必须以'/'开头      

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

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



    三. 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", " ");
就解决了问题。

 

转载于:https://my.oschina.net/leoson/blog/103245

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值