一、首先看一下File中參數的集中形式
①//構造函數File(String pathname)
File f1 = new File("c:\\1.txt");
②//File(String parent,String child)
File f2 = new File("c:\\abc","2.txt");
③//File(File parent,String child)
File f3 =new File("c:"+File.separator+"abc");//separator 是跨平臺分隔符
File f4 =new File(f3,"3.txt");
二、File f1 能夠定義絕對路徑,也能夠定義相對路徑
三、區分相對和絕對
①絕對路徑:
a.帶盤符,如E:/book.xml
b.以http開頭,http://img.baidu.com/img/book.jpg
②相對路徑:
a.帶
"/"
開頭,如/book.xml
b.不帶
"/"
開頭,如book.xml
帶"/"開頭,如/book.xml不帶"/"開頭,如book.xml
四、相對和絕對的校驗方式:
四、相對和絕對的校驗方式:
public String filter3(String param){
if(param!=null || param!=""){
if(":".equals(param.indexOf(1,2))){//是絕對路徑
param = param.replaceAll("../", "").replaceAll("./", "");
String params [] = param.substring(2,param.length()).split("/");
String aa = "";
if(params.length>0){
for(int i=0;i<params.length;i++){
//正則表達式:/ * : < > ?
Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");
Matcher matcher = pattern.matcher(params[i]);
aa += matcher.replaceAll("") + "/" ;
}
}
param = param.substring(0,2) + aa ;
param = param.substring(0,param.length()-1);
}else if("http:".equals(param.toLowerCase().indexOf(0,5))){
param = param.replaceAll("../", "").replaceAll("./", "");
String params [] = param.substring(5,param.length()).split("/");
String aa = "";
if(params.length>0){
for(int i=0;i<params.length;i++){
//正則表達式:/ * : < > ?
Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");
Matcher matcher = pattern.matcher(params[i]);
aa += matcher.replaceAll("") + "/" ;
}
}
param = param.substring(0,5) + aa ;
param = param.substring(0,param.length()-1);
}else{//是相對路徑
param = param.replaceAll(":", "");
String params [] = param.substring(5,param.length()).split("/");
String aa = "";
if(params.length>0){
for(int i=0;i<params.length;i++){
//正則表達式:/ * : < > ?
Pattern pattern = Pattern.compile("[\\s\\\\/:\\*\\?\\\"<>\\|]");
Matcher matcher = pattern.matcher(params[i]);
aa += matcher.replaceAll("") + "/" ;
}
}
param = param.substring(0,5) + aa ;
param = param.substring(0,param.length()-1);
}
}
return param;
}正則表達式