티스토리 뷰
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();
}
}
결과
'프로그래밍 언어 > Java Programming' 카테고리의 다른 글
URLConnection 으로 웹서버에 데이터 보내보기 (0) | 2016.10.22 |
---|---|
JAVA 왜 버퍼(buffer)를 사용해야 하나요? (0) | 2016.10.10 |
JAVA(자바) IO(input output) stream (0) | 2016.09.21 |
Java 컬렉션(Collection) 정리 (0) | 2016.09.20 |
InetAddress class host name으로 정보 얻기 (0) | 2016.09.18 |