zoukankan      html  css  js  c++  java
  • java上传文件获取跟目录的办法

    在java中获得文件的路径在我们做上传文件操作时是不可避免的。
    web 上运行
    1:
    this.getClass().getClassLoader().getResource("/").getPath();
    this.getClass().getClassLoader().getResource("").getPath();  得到的是 ClassPath的绝对URI路径。
    如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
    System.getProperty("user.dir");
    this.getClass().getClassLoader().getResource(".").getPath();    得到的是 项目的绝对路径。
    如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

    2:
    this.getClass().getResource("/").getPath();
    this.getClass().getResource("").getPath(); 得到的是当前类文件的URI目录。不包括自己!
    如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
    this.getClass().getResource(".").getPath();   X 不 能运行

    3:
    Thread.currentThread().getContextClassLoader().getResource("/").getPath()
    Thread.currentThread().getContextClassLoader().getResource("").getPath()  得到的是 ClassPath的绝对URI路径。
    如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
    Thread.currentThread().getContextClassLoader().getResource(".").getPath()  得到的是 项目的绝对路径。
    如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

    在本地运行中
    1:
    this.getClass().getClassLoader().getResource("").getPath();
    this.getClass().getClassLoader().getResource(".").getPath();   得到的是 ClassPath的绝对URI路径。
    如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
    this.getClass().getClassLoader().getResource(".").getPath();  X 不 能运行
    2:
    this.getClass().getResource("").getPath();
    this.getClass().getResource(".").getPath(); 得到的是当前类文件的URI目录。不包括自己!
    如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/
    /D:/myProjects/hp/WebRoot/WEB-INF/classes/    得到的是 ClassPath的绝对URI路径。
    如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
    3:

    Thread.currentThread().getContextClassLoader().getResource(".").getPath()
    Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。。
    如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
    Thread.currentThread().getContextClassLoader().getResource("/").getPath()    X 不 能运行



     
    最后
        在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
    还有request.getContextPath();  在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiuhaijuanqiang/archive/2011/01/14/6137949.aspx

  • 相关阅读:
    设计模式---单例模式
    Linux介绍
    集合---Map
    集合---Collection
    JDK1.7中HashMap底层实现原理(转)
    算法面试题-用单向链表表示十进制整数,求两个正整数之和。1234+34=1268
    idea 常用插件
    ehcache配置log4j日志,或与spirng cache整合用注解形式,打印注解日志
    spring cache @CacheEvict 清除多个key
    tomcat源码及其下载版本
  • 原文地址:https://www.cnblogs.com/laobiao/p/5538760.html
Copyright © 2011-2022 走看看