上傳文件路徑為:C:/Documents and Settings/collin/My Documents/111-lazyloading.gif,欲取出文件名:111-lazyloading.gif。可以
String temp[] = name.split("////");
if (temp.length > 1) {
name = temp[temp.length - 1];
}
regex為////,因?yàn)樵?a href="/article/17576.html">java中//表示一個(gè)/,而regex中//也表示/,所以當(dāng)////解析成regex的時(shí)候?yàn)?/。
由于unix中file.separator為斜杠"/",下面這段代碼可以處理windows和unix下的所有情況:
String temp[] = name.replaceAll("////","/").split("/");
if (temp.length > 1) {
name = temp[temp.length - 1];
}