tomcat相对路径

String tomcat_path = System.getProperty("user.dir").replace("\\""/") +"/../webapps/项目名称";//这个方法可以取得tomcat下你的项目的绝对路径

File file = new  File(tomcat_path+"你的文件名");



// FrontEnd Plus GUI for JAD // DeCompiled : PropertyManager.class package tools; import java.io.*; import java.util.*; public class PropertyManager { private static PropertyManager manager = null; private static Object managerLock = new Object(); private static String propsName = "/elearning.properties"; private Properties properties; private Object propertiesLock; private String resourceURI; public PropertyManager() { properties = null; propertiesLock = new Object(); } public static String getProperty(String s) { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.getProp(s); } public static void setProperty(String s, String s1) { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } manager.setProp(s, s1); } public static void deleteProperty(String s) { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } manager.deleteProp(s); } public static Enumeration propertyNames() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propNames(); } public static boolean propertyFileIsReadable() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propFileIsReadable(); } public static boolean propertyFileIsWritable() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propFileIsWritable(); } public static boolean propertyFileExists() { if (manager == null) synchronized (managerLock) { if (manager == null) manager = new PropertyManager(propsName); } return manager.propFileExists(); } private PropertyManager(String s) { properties = null; propertiesLock = new Object(); resourceURI = s; } protected String getProp(String s) { if (properties == null) synchronized (propertiesLock) { if (properties == null) loadProps(); } String s1 = properties.getProperty(s); if (s1 == null) return null; else return s1.trim(); } protected void setProp(String s, String s1) { synchronized (propertiesLock) { if (properties == null) loadProps(); properties.setProperty(s, s1); saveProps(); } } protected void deleteProp(String s) { synchronized (propertiesLock) { if (properties == null) loadProps(); properties.remove(s); saveProps(); } } protected Enumeration propNames() { if (properties == null) synchronized (propertiesLock) { if (properties == null) loadProps(); } return properties.propertyNames(); } private void loadProps() { properties = new Properties(); InputStream inputstream = null; try { inputstream = getClass().getResourceAsStream(resourceURI); properties.load(inputstream); } catch (Exception exception) { exception.printStackTrace(); } finally { try { inputstream.close(); } catch (Exception exception2) { } } } private void saveProps() { String s = properties.getProperty("path").trim(); FileOutputStream fileoutputstream = null; try { fileoutputstream = new FileOutputStream(s); properties.store(fileoutputstream, "elearning.properties -- " + new Date()); } catch (Exception exception) { exception.printStackTrace(); } finally { try { fileoutputstream.close(); } catch (Exception exception2) { } } } public boolean propFileIsReadable() { try { InputStream inputstream = getClass().getResourceAsStream( resourceURI); return true; } catch (Exception exception) { return false; } } public boolean propFileExists() { /*  * String s = getProp("path"); if (s == null) return false; File file =  * new File(s); return file.isFile();  */ return true; } public boolean propFileIsWritable() { String s = getProp("path"); File file = new File(s); if (file.isFile()) return file.canWrite(); else return false; } public static void main(String[] args) { System.out.println(PropertyManager.getProperty("DEFAULTALIAS_2")); } }








第一步:

Eclipse新建一个Dynamic Web Project,设想是以Mayaa+Html的方式来处理Web的表示层,方便进行页面设计。

目录结构如下:

WebContent

      +css

      +img

      -WEB-INF

         -index.html

         -index.mayaa

 

第二步:

用Dreamweaver进行html页面的设计,相关的图片和css文件都是相对路径(如:../css/style.css),页面设计完成以后,能正常在浏览器里显示,也能在Eclipse正常预览。

 

第三步:

回到Eclipse做一些动态处理,然后Run on server,启动Tomcat,运行Web。

 

此时,问题出现了,通过Tomcat运行的index.html无法找到css和image资源,最初以为是mayaa的问题,结果发现,把浏览器中显示不正确的页面保存覆盖index.html后,页面能正常显示了,以此得知,还是相对路径的设置问题。

有个想法闪过,既然网站根目录是WebContent,那么何不试试把路径改成css/style.css呢,果然,这样就能正常显示了。

但是在Eclipse和Dreamweaven中都不能可视化编写了,只能再手动把路径改过来。

虽然有点麻烦,暂时就先这样处理了。

 

 

//获得上下文
DirContext dirContext = DirContextURLStreamHandler.get();
//查询templates这个目录资源
Object obj = dirContext.lookup("/templates");
FileDirContext fdc;
String webrootPath;
// 判断取出来的对象是不是FileDirContext
// 也就是判断这个资源名对应的资源是不是目录资源
if (obj instanceof FileDirContext) {
    fdc = (FileDirContext) obj;
    // 获取web项目根目录的绝对路径
    webrootPath = fdc.getDocBase();
}

String templatesPath = webrootPath + "/tempaltes";
上面的代码先获得目录上下文,然后获得目录资源。这里判断了一下获得Object是不是FileDirContext,在jndi中,所有的目录资源都被 抽象成FileDirContext这个对象。如果是文件就会被抽象成FileDirContext.FileResource这个对象。
  如果不是目录资源,比如是一个文件资源,那返回的ProxyDirContext这个对象,获得文件资源的方法在后面讲。
  但是,看apache的api,会发现你是没有办法拿到FileResource这个对象,因为目前的api没有暴露出来。虽然在debug的时候会发 现,FileDirContext里面已经有属性(具体那个属性忘了)保存着我想要的东西了,但是就是没有办法通过api来得到这个东西。着实郁闷了一 把。
  我尝试了各种方法,看了几遍api,都没有找到能够从FileDirContext里面直接获取我想要的属性的方法(也就是templates目录的绝对 路径)。最后发现FileDirContext有个getDocBase()方法,能够拿到web项目的根目录的绝对路径。
  后来我想了一下,其实拿到这个根目录的绝对路径基本也就足够了,因为我们在web项目中指定资源的相对路径时,通常都是从webroot开始的。估计也是因为这个,大家在制定相对路径时都从webroot开始制定。
  拿到webroot之后,就可以拼接上我的模板目录的相对路径,然后获得模板目录的绝对路径了。为了方便,可以把这个相对路径做成Bean的一个属性,然后在Spring的配置文件中设定。如果想更方便,那就用maven。在一个外部文件中设定这个值。
  下面来说说获取文件资源,获取文件资源的思路基本相同,只不过换了另外一个类而已。如果是文件资源可以用下面的代码:

//获得上下文
DirContext dirContext = DirContextURLStreamHandler.get();
//查询templates这个目录资源
Object obj = dirContext.lookup("/templates/test.vm");
FileDirContext fdc;
String webrootPath;
// 判断取出来的对象是不是FileDirContext
// 也就是判断这个资源名对应的资源是不是目录资源
if (obj instanceof ProxyDirContext) {
    fdc = (FileDirContext) obj;
    // 获取web项目根目录的绝对路径
    webrootPath = fdc.getDocBase();
}

String templatesPath = webrootPath + "/tempaltes/test.vm";
区别就在红色标注的9,15两行代码,lookup返回的类不一样了,变成ProxyDirContext了。ProxyDirContext中也有一个getDocBase方法,通过它拿到web项目的绝对路径,然后拼接上文件资源的相对路径也就拿到了完整的路径。

posted on 2013-11-01 01:17  笨'小孩  阅读(2719)  评论(0编辑  收藏  举报

导航