linux与windows获得项目的路径及创建下级目录

来源:互联网 发布:mac电脑如何强制关机 编辑:程序博客网 时间:2024/06/05 19:23

 String filePath = session.getServletContext().getRealPath("/");

windows下获得的路径为weblogic部署的项目路径如:D:\Middleware\user_projects\domains\base_domain\autodeploy\项目名称\

然而是linux下获得的路径没有项目名称后面的反斜杠“\”

所以为了保证linux和windows环境下生成的目录相同,则需要判断当前的系统名称

 String filePath = session.getServletContext().getRealPath("/")+"Image";//默认windows文件路径,linux环境下生成的目录与项目同级,而不是下级
        
         Properties pro = System.getProperties();
         String osName = pro.getProperty("os.name");//获得当前操作系统的名称

         if("Linux".equals(osName) || "linux".equals(osName) || "LINUX".equals(osName)){
          filePath = session.getServletContext().getRealPath("/")+"/Image"; //linux环境下的路径
         }

 File tempDir = new File(filePath);

原创粉丝点击