Theo phương pháp URI javadoc phương thức getPath
trả về "thành phần đường dẫn đã giải mã của URI này, hoặc null nếu đường dẫn không được xác định" (nhấn mạnh được thêm). Điều này sẽ khiến tôi tin rằng nếu đơn đăng ký của tôi dựa trên giá trị trả lại là getPath
, tôi có thể cần kiểm tra xem đó có phải là null
hay không. Tuy nhiên, có vẻ như điều này không bao giờ có thể xảy ra.Có thể nào cho phương thức getPath của đối tượng java.net.URI trả về null không? (Nếu có, khi nào?)
Đoạn mã sau cho thấy nỗ lực của tôi để xây dựng một đối tượng URI
mà getPath
trả về null, nhưng như bạn thấy, tôi chưa tìm thấy trường hợp như vậy. Ai đó có thể vui lòng giải thích làm thế nào điều này có thể xảy ra?
CHỈNH SỬA: Tôi nhận thấy rằng một URI mailto
không có đường dẫn. Tuy nhiên, điều tôi thực sự hỏi là: Có thể có một URI sử dụng lược đồ http, https, ftp hoặc tệp có đường dẫn không xác định/không?
import java.net.URI;
import java.net.URISyntaxException;
public class URIGetPathNullTest {
public static void main(String []args) throws Exception {
test1();
test2();
test3();
test4();
test5();
test6();
test7();
}
public static void test1() throws URISyntaxException {
String urlString = "";
URI uri = new URI(urlString);
printUri(uri);
// Output:
// toString() -->
// getPath -->
// getPath null? false
}
public static void test2() throws URISyntaxException{
String scheme = null;
String ssp = null;
String fragment = null;
URI uri = new URI(
scheme,
ssp,
fragment
);
printUri(uri);
// Output:
// toString() -->
// getPath -->
// getPath null? false
}
public static void test3() throws URISyntaxException {
String scheme = null;
String userInfo = null;
String host = null;
int port = -1;
String path = null;
String query = null;
String fragment = null;
URI uri = new URI(
scheme,
userInfo,
host,
port,
path,
query,
fragment
);
printUri(uri);
// Output:
// toString() -->
// getPath -->
// getPath null? false
}
public static void test4() throws URISyntaxException {
String scheme = null;
String host = null;
String path = null;
String fragment = null;
URI uri = new URI(
scheme,
host,
path,
fragment
);
printUri(uri);
// Output:
// toString() -->
// getPath -->
// getPath null? false
}
public static void test5() throws URISyntaxException {
String scheme = null;
String authority = null;
String path = null;
String query = null;
String fragment = null;
URI uri = new URI(
scheme,
authority,
path,
query,
fragment
);
printUri(uri);
// Output:
// toString() -->
// getPath -->
// getPath null? false
}
public static void test6() throws URISyntaxException {
String urlString = "?some-query";
URI uri = new URI(urlString);
printUri(uri);
// Output:
// toString() --> ?some-query
// getPath -->
// getPath null? false
}
public static void test7() throws URISyntaxException {
String urlString = "#some-fragment";
URI uri = new URI(urlString);
printUri(uri);
// Output:
// toString() --> #some-fragment
// getPath -->
// getPath null? false
}
public static void printUri(URI uri) {
System.out.println("toString() --> " + uri.toString());
System.out.println("getPath --> " + uri.getPath());
System.out.println("getPath null? " + (uri.getPath() == null));
}
}
Nice one. Tôi đã học được điều gì đó mới mẻ. Cảm ơn. :) –