1. 程式人生 > >java web專案獲取window/linux真實路徑

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; }