java web项目获取window/linux真实路径

通用工具类

  • java web 项目 真实 路径 获取 windows linux 系统 :

    这是在web项目中,获取项目实际路径的最佳方式,在windows和linux系统下均可正常使用:

public class GetMsgCenterKey {

public static String getRootPath() {
    String line=File.separator;                      
    String path=Thread.currentThread().getContextClassLoader().getResource("").toString();  
            //windows下
            if("\\".equals(line)){
                path = path.replace("/", "\\");  // 将/换成\\
                path1=path+line+"excel"+line+time+".xls";
                download=line+"excel"+line+time+".xls";
            }
           //linux下
            if("/".equals(line)){
                 path = path.replace("\\", "/");
                 path1=path+line+"excel"+line+time+".xls";
                 download=line+"excel"+line+time+".xls";
            }
            try {   
                    System.out.println("开始导出");
                    out = new FileOutputStream(path1);
                    //ExcelAction ex=new ExcelAction();
                    //ex.do_ExportExcel_listmap(fleld,rs,out,dataset_fields);
                    //map.put("msg", "success");    
                    } catch (FileNotFoundException e) {
                        System.out.println("错误");
                        //map.put("msg", "error");
                        e.printStackTrace();
                }  
         }
        System.out.println(path1);
        //map.put("path", download);
        return path1;
    }

你可能感兴趣的:(javaWeb,window,linux)