URL 클래스는 Write-Once Object로써 생성된후 어떠한 속성도 바꿀수가 없는 것이 특징 입니다. URL클래스는 인터넷 상의 자원인 URL객체로 나타낸다고 보면 되는데 이 클래스를 통해 프로토콜, 호스트이름, 포트번호등을 알아 낼수 있습니다. 생성자는 다음과 같습니다. ----------------- URL Constructors ----------------- new URL("http://www.abc.com:80/"); new URL("http", "www.abc.com", "/index.html"); new URL("http", "www.abc.com", 80, "pages/index.html");
제공되는 메소드는 다음과 같습니다.
public String getProtocol() Returns the protocol identifier component of the URL. public String getHost() Returns the host name of the URL. public int getPort() Returns the port number component of the URL. public String getFile() Returns the filename of the URL. public Object getContent() Gets the contents of this URL public Object getContent(Class[] classes) Gets the contents of this URL
매개변수로 들어온 class의 인스턴스로 만들어진 자바의 URL 객체 컨텐츠를 반환한다. 클래스가 지원되지 않는 경우는 null 반환한다. 이 메소드를 호출한 쪽에서 어떻게 content를 인코딩 할지를 조절 할 수 있다. 예를들면 현재의 URL은 HTML 웹페이지의 인코딩을 String, HTML객체, InputStream 타입으로 수행할 수 있도록 지원하고 있는데 이때 URL 리소스를 문자열로 인코딩하여 받고 싶다면 이 메소드에 String.class를 넘기면 되고 , 스트림으로 인코딩 하여 받고 싶다면 InputStream.class를 넘기면 된다. 즉 이 메소드는 주어진 URL에서 여러가지 컨텐트 인코딩을 지원하는 경우에 쓸모가 있다. public Object getContent() 의 경우 반환되는 content의 type은 해당 컨텐츠에 달려 있다. 만약 URL이 어떤 이미지를 가리키고 있다면 반환되는 타입은 Image 가 될것이다.
public final InputStream openStream() throws IOException Opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for openConnection().getInputStream()
public URLConnection openConnection() throws IOException Returns a URLConnection object that represents a connection to the remote object referred to by the URL. A new connection is opened every time by calling the openConnection method of the protocol handler for this URL.
--------------- 예제1 --------------- import java.io.*; import java.net.*;
class URLInfoTest{ public static void main(String[] args) throws Exception { try{ //URL aURL = new URL("http://www.oraclejava.co.kr"); URL aURL = new URL("http", "www.oraclejava.co.kr", 80 , "/index.jsp"); System.out.println("protocol name:" + aURL.getProtocol()); System.out.println("host name:" + aURL.getHost()); System.out.println("file name:" + aURL.getFile()); //생성자에서 port가 set되는 경우에 표시, 아니면 -1 System.out.println("port name:" + aURL.getPort()); //HTML 문서 내부의 위치를 지정하는 <a name="..."> 태그의 값을 구한다. System.out.println("ref : "+aURL.getRef()); BufferedReader br = new BufferedReader(new InputStreamReader(aURL.openStream())); String line; while((line = br.readLine()) != null) { System.out.println(line); } } catch(MalformedURLException e) { System.out.println("MalformedURLException : "+e); } catch(IOException e) { System.out.println("IOException :"+ e); } } }
----------------- 예제2 ----------------- import java.io.*; import java.net.*;
class URLInfoTest2{ public static void main(String[] args) throws Exception { try{ int ch; URL aURL = new URL("http", "www.oraclejava.co.kr", 80 , "/test.html"); Class[] classes = {Reader.class, InputStream.class}; Object content = aURL.getContent(classes); System.out.println(content + "\n"); while((ch=((InputStream)content).read()) != -1) { System.out.print((char) ch); } System.out.println("--------------------------------"); InputStream is = aURL.openStream(); while((ch=is.read()) != -1) { System.out.print((char) ch); } } catch(MalformedURLException e) { System.out.println("MalformedURLException : "+e); } catch(IOException e) { System.out.println("IOException :"+ e); } } }
-------------------- 예제3 -------------------- import java.io.*; import java.net.*; public class PageReadTest{ public static void main(String[] args) throws Exception { if(args.length<1) System.out.println("usage: java PageReadTest http://www.oraclejava.co.kr"); URL location = new URL(args[0].toString()); BufferedReader in = new BufferedReader (new InputStreamReader(location.openStream())); String readLine; while ((readLine = in.readLine()) != null) System.out.println(readLine); in.close(); } }
| |