본문 바로가기

명사 美 비격식 (무리 중에서) 아주 뛰어난[눈에 띄는] 사람[것]

JAVA

자원의 위치를 식별하는 데 사용되는 클래스URL와 URL메서드: URL() getAuthority() getContent() getDefaultPort() getFile() getHost() getPath() getPort() getProtocol() getQuery() getRef() getUserInfo() openConnection() openStream() set() toExternalForm() toURL()

URL

Uniform Resource Locator

자원의 위치를 식별하는 데 사용되는 클래스

웹상의 자원을 가리키는 문자열로부터 해당 자원의 프로토콜, 호스트, 포트, 경로 등을 추출하고 관리

자바 표준 라이브러리(java.net 패키지)에 포함되어 있으며, 네트워크 프로그래밍 및 웹 개발에서 많이 활용된다.

 

 

주요기능을 살펴보자.

 

자원 위치 식별

문자열 형태로 표현된 자원의 위치(웹 페이지, 이미지, 파일 등)를 식별

구성 요소 추출

주어진 문자열에서 프로토콜, 호스트, 포트, 경로, 쿼리 등의 구성 요소를 추출

프로토콜 지원
다양한 프로토콜(HTTP, HTTPS, FTP 등)을 지원하여 해당 프로토콜을 사용하여 자원에 접근
연결 및 통신

URL 객체를 사용하여 원격 호스트에 연결하고 자원을 가져오는 등의 네트워크 통신 작업을 수행
URL 구성 변경
URL 문자열의 구성 요소를 변경하고 새로운 URL 객체를 생성하는 메서드를 제공

프록시 지원
프록시 서버를 사용하여 URL에 연결하는 기능을 제공

 

 

URL 메서드를 확인해보자.

 

URL(String spec)
지정된 URL 문자열에서 URL을 생성합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource?query=value#section");

        // URL을 외부 형식의 문자열로 반환
        String externalForm = url.toExternalForm();

        // 출력 결과 출력
        System.out.println("toExternalForm(): " + externalForm);
    }
}

// toExternalForm(): https://www.example.com/path/to/resource?query=value#section

 

 

URL(String protocol, String host, String file)
지정된 프로토콜, 호스트, 파일 경로에서 URL을 생성합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https", "www.example.com", "/path/to/resource");

        // URL의 권한 부분 반환
        String authority = url.getAuthority();

        // 출력 결과 출력
        System.out.println("getAuthority(): " + authority);
    }
}

//getAuthority(): www.example.com

 

 

String getAuthority()
URL의 권한 부분을 반환합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 권한 부분 반환
        String authority = url.getAuthority();

        // 출력 결과 출력
        System.out.println("getAuthority(): " + authority);
    }
}
//getAuthority(): www.example.com

 

 


Object getContent()
URL의 콘텐츠를 읽어서 Object로 반환합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 콘텐츠를 읽어서 Object로 반환
        Object content = url.getContent();

        // 출력 결과
        System.out.println("Content: " + content);
    }
}
//Content: sun.net.www.protocol.https.HttpsURLConnectionImpl$HttpUrlConnectionImpl@1efab9e

 

 

 


int getDefaultPort()
URL의 기본 포트 번호를 반환합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 기본 포트 번호 반환
        int defaultPort = url.getDefaultPort();

        // 출력 결과
        System.out.println("Default Port: " + defaultPort);
    }
}

//출력 결과: 443

 

 

 

String getFile()
URL의 파일 부분을 반환합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 파일 부분 반환
        String file = url.getFile();

        // 출력 결과
        System.out.println(file);
    }
}


//출력 결과: /path/to/resource

 

 

String getHost()
URL의 호스트 이름을 반환합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 호스트 이름 반환
        String host = url.getHost();

        // 출력 결과
        System.out.println(host);
    }
}
//출력 결과: www.example.com

 

 

 

String getPath()
URL의 경로 부분을 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 경로 부분 반환
        String path = url.getPath();

        // 출력 결과
        System.out.println(path);
    }
}

출력 결과: /path/to/resource

 

 

 

 

int getPort()
URL의 포트 번호를 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com:8080/path/to/resource");

        // URL의 포트 번호 반환
        int port = url.getPort();

        // 출력 결과
        System.out.println(port);
    }
}
출력 결과: 8080

 

 

 

String getProtocol()
 URL의 프로토콜을 반환합니다.

import java.net.URL;

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 프로토콜 반환
        String protocol = url.getProtocol();

        // 출력 결과
        System.out.println(protocol);
    }
}

출력 결과: https

 

 


String getQuery()
 URL의 쿼리 부분을 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource?query=value");

        // URL의 쿼리 부분 반환
        String query = url.getQuery();

        // 출력 결과
        System.out.println(query);
    }
}

출력 결과: query=value

 

 

String getRef()
 URL의 참조 부분을 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource#section");

        // URL의 참조 부분 반환
        String ref = url.getRef();

        // 출력 결과
        System.out.println(ref);
    }
}

출력 결과: section

 

 

String getUserInfo()

 URL의 사용자 정보 부분을 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://user:password@www.example.com/path/to/resource");

        // URL의 사용자 정보 부분 반환
        String userInfo = url.getUserInfo();

        // 출력 결과
        System.out.println(userInfo);
    }
}

출력 결과: user:password

 

 

 

URLConnection openConnection()
 URL을 연결하고 URLConnection 객체를 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com");

        // URL 연결 및 URLConnection 객체 반환
        URLConnection connection = url.openConnection();

        // 출력 결과
        System.out.println("URLConnection 객체 생성됨: " + connection);
    }
}


출력 결과: URLConnection 객체 생성됨: sun.net.www.protocol.https.HttpsURLConnectionImpl@2f7d6d67 (예시입니다. 실제 출력 값은 다를 수 있습니다.)

 

 

 

URLConnection openConnection(Proxy proxy)
 지정된 프록시를 사용하여 URL을 연결하고 URLConnection 객체를 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // 프록시 설정
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new java.net.InetSocketAddress("proxy.example.com", 8080));

        // URL 객체 생성
        URL url = new URL("https://www.example.com");

        // 프록시를 사용하여 URL 연결 및 URLConnection 객체 반환
        URLConnection connection = url.openConnection(proxy);

        // 출력 결과
        System.out.println("Proxy를 사용하여 URLConnection 객체 생성됨: " + connection);
    }
}

출력 결과: Proxy를 사용하여 URLConnection 객체 생성됨: sun.net.www.protocol.https.HttpsURLConnectionImpl@2f7d6d67 (예시입니다. 실제 출력 값은 다를 수 있습니다.)

 

 

 

InputStream openStream()
 URL의 콘텐츠를 읽어오는 InputStream을 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com");

        // URL의 콘텐츠를 읽어오는 InputStream 반환
        InputStream inputStream = url.openStream();

        // 출력 결과
        System.out.println("InputStream 객체 생성됨: " + inputStream);
    }
}

출력 결과: InputStream 객체 생성됨: sun.net.www.protocol.https.HttpsURLConnectionImpl$HttpInputStream@2f7d6d67 (예시입니다. 실제 출력 값은 다를 수 있습니다.)

 

 

void set(String protocol, String host, int port, String file, String ref)
 URL의 프로토콜, 호스트, 포트, 파일, 참조 부분을 설정합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 프로토콜, 호스트, 포트, 파일, 참조 부분 설정
        url.set("http", "example.org", 8080, "/new/path", "section");

        // 출력 결과
        System.out.println("URL 변경 후: " + url);
    }
}

출력 결과: URL 변경 후: http://example.org:8080/new/path#section

 

 

void set(String protocol, String host, int port, String authority, String userInfo, Stripath, String query, String ref)
URL의 여러 부분을 설정합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // URL의 여러 부분 설정
        url.set("http", "example.org", 8080, "www.example.org", "user:password", "/new/path", "query=value", "section");

        // 출력 결과
        System.out.println("URL 변경 후: " + url);
    }
}


출력 결과: URL 변경 후: http://user:password@example.org:8080/new/path?query=value#section

 

 

String toExternalForm()
 URL을 외부 형식의 문자열로 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 객체 생성
        URL url = new URL("https://www.example.com/path/to/resource");

        // 외부 형식의 문자열로 URL 반환
        String externalForm = url.toExternalForm();

        // 출력 결과
        System.out.println("URL 외부 형식의 문자열: " + externalForm);
    }
}

출력 결과: URL 외부 형식의 문자열: https://www.example.com/path/to/resource

 

 

URL toURL()
 URL 객체를 반환합니다.

public class Main {
    public static void main(String[] args) throws Exception {
        // URL 문자열
        String urlString = "https://www.example.com/path/to/resource";

        // URL 객체 생성
        URL url = new URL(urlString);

        // URL 객체 반환
        URL newURL = url.toURL();

        // 출력 결과
        System.out.println("새로운 URL 객체: " + newURL);
    }
}

출력 결과: 새로운 URL 객체: https://www.example.com/path/to/resource