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

来源:互联网 发布:购买域名 编辑:程序博客网 时间:2024/04/29 18:50

、#通用工具类

- 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;    }
0 0
原创粉丝点击