호스트 이름(URL)을 IP 주소로 반환
package ex17;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Host2Ip {
public static void main(String[] args) {
String hostname = "www.naver.com";
try {
InetAddress address = InetAddress.getByName(hostname);
System.out.println("IP 주소 : " + address.getHostAddress());
} catch (UnknownHostException e) {
System.out.println(hostname + "의 IP 주소를 찾을 수 없습니다.");
}
}
}
// IP 주소 : 223.130.200.104
인터넷에서 파일 다운로드
package ex17;
// 네이버 에서 데이터를 읽어서 콘솔에 표시하는 프로그램
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL site = new URL("<https://www.naver.com/>");
URLConnection url = site.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(url.getInputStream())
);
String inLine;
while ((inLine = in.readLine()) != null) {
System.out.println(inLine);
}
in.close();
}
}