文件路徑的讀取

String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); 
 int num=t.indexOf(".metadata");

 String path=t.substring(1,num).replace('/', '\\')+"項目名\\WebContent\\文件";

//maven項目部署之後的文件路徑讀取

 String FILE_SEPARATOR = System.getProperty("file.separator");  
 String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); 
 String projectName = request.getServletContext().getContextPath();
 int num=t.indexOf(projectName);
 String path=t.substring(0,num)+projectName+FILE_SEPARATOR+"export";

 File f = new File(path.replace("/", FILE_SEPARATOR).replace("\\", FILE_SEPARATOR));

/maven項目本地環境的文件路徑讀取

String t=Thread.currentThread().getContextClassLoader().getResource("").getPath(); 
String projectName = request.getServletContext().getContextPath();
int num=t.indexOf(projectName);
String path=t.substring(1,num).replace('/', '\\')+projectName+"\\src\\main\\webapp\\export";
String path=t.substring(0,num)+projectName+FILE_SEPARATOR+"export";
File f = new File(path.replace("/", FILE_SEPARATOR).replace("\\", FILE_SEPARATOR));


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章