Java – 如何查詢URI或是URL的最後一個路徑 讓我告訴你!
大家在寫JAVA是否在處理路徑的時候需要取URI的最後一個名稱,
想不到URL的最後一個路徑要怎麼取得,
我在這邊紀錄一下我取得的過程!
URI uri = new URI("https://example.com/test/test2/123?param=true");
String path = uri.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
int id = Integer.parseInt(idStr);
或是
URI uri = new URI("https://example.com/test/test2/123?param=true");
String[] segments = uri.getPath().split("/");
String idStr = segments[segments.length-1];
int id = Integer.parseInt(idStr);