티스토리 뷰

URL 클래스

특징

  • URL은 WWW의 자원인 Uniform Resource Locator를 나타냅니다.
  • Resource는 파일이나 디렉토리 같은 단순한 것일 수 있고 DB같은 복잡한 개체에 대한 참조일 수 있습니다.

 

주요 메소드

String getAuthority()

Gets the authority part of this URL.

Object getContent()

Gets the contents of this URL.

Object getContent(Class[] classes)

Gets the contents of this URL.

int getDefaultPort()

Gets the default port number of the protocol associated with this URL.

String getHost()

Gets the host name of this URL, if applicable.

String getPath()

Gets the path part of this URL.

String getProtocol()

Gets the protocol name of this URL.

String getUserInfo()

Gets the userInfo part of this URL.

URLConnection openConnection()

Returns a URLConnection instance that represents a connection to the remote object referred to by the URL.

URLConnection openConnection(Proxy proxy)

Same as openConnection(), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter and make a normal connection.

InputStream openStream()

Opens a connection to this URL and returns an InputStream for reading from that connection.

static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac)

Sets an application's URLStreamHandlerFactory.

URI toURI()

Returns a URI equivalent to this URL.

 

사용예제

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.io.InputStreamReader;

public class Main2 {
    public static void main(String[] args) throws IOException {

        URL url = new URL("https://www.naver.com");
        String protocol = url.getProtocol();
        String host = url.getHost();
        int port = url.getPort();

        System.out.println(protocol);
        System.out.println(host);
        System.out.println(port);

        url.openStream();
        InputStream ins = url.openStream();
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(ins));

        String str="";
        while((str=br.readLine())!=null){
            System.out.println(str);
        }

        br.close();
        ins.close();
    }
}

 

결과

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함