일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- web
- 웹
- 객체지향
- 테이블
- javascript
- Oracle
- Java
- 객제지향프로그래밍
- 주말이다..
- 자바
- 객체지향프로그래밍
- CSS
- 코딩
- UI
- sql
- html
- 객제지향
- squery
- DB
- ERWin
- 웹프로그래밍
- 데이터베이스
- 오라클
- jsp
- orcle
- 프로그래밍
- 프로젝트
- 공부를열심히
- 공부
- Project
Archives
- Today
- Total
햄찌개
JAVA- javaNetWork 본문
public class InetAddressTest {
public static void main(String[] args) throws IOException {
// InetAddress 클래스 => IP주소를 다루기 위한 클래스
// naver 사이트의 ip정보 가져오기
InetAddress naverIp = InetAddress.getByName("www.naver.com");
//호스트 이름은 머신이름 , 도메인명, 또는 ip주소 문자열.
System.out.println("Host Name : "+naverIp.getHostName());
System.out.println("Host Address : "+naverIp.getHostAddress());
System.out.println();
//자기 자신 컴퓨터의 Ip주소
InetAddress localIp = InetAddress.getLocalHost();
System.out.println("내 컴퓨터의 Host Name : "+localIp.getHostName());
System.out.println("내 컴퓨터의 Host Address : "+localIp.getHostAddress());
System.out.println();
//ip주소가 여러개인 호스트의 정보 가져오기
InetAddress[] naverIps = InetAddress.getAllByName("www.naver.com");
for (InetAddress nIp : naverIps) {
System.out.println(nIp.toString());
}
}
}
public class URLTest {
public static void main(String[] args) throws IOException, URISyntaxException {
//URL 클래스 => 인터넷에서 존재하는 서버들의 자원에 접근할 수 있는 주소를 관리하는 클래스
//http://ddit.or.kr:80//iindex.html?ttt=123
URL url = new URL("http","ddit.or.kr",80,"main/index.html?ttt=123#kkk");
System.out.println("전체 URL 주소 : http://ddit.or.kr:80/index.html?ttt=123#kkk");
System.out.println("protocol : "+url.getProtocol() );
System.out.println("host : "+url.getHost() );
System.out.println("file : "+url.getFile() );//쿼리정보 포함
System.out.println("query : "+url.getQuery() );
System.out.println("path : "+url.getPath() );//쿼리정보 미포함
System.out.println("port : "+url.getPort() );
System.out.println("ref : "+url.getRef() );
System.out.println(url.toExternalForm());
System.out.println(url.toString());
System.out.println(url.toURI().toString());
//URL예제
/**
* http://java.sun.com/j2se/1.3/
* docs/guide/collections/designfeq.html#28
* ../.,./../demo/ifc/src/hello.java
* file:///~/calender
* mailto:java-net@abc.com
*/
}
}
package kr.or.ddit.basic;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class URLConnectionTest {
public static void main(String[] args) throws IOException {
//URLConnection => 애플리케이션과 URL간의 통신 연결을 위한 추상 클래스
//특정 서버(예:naver서버)의 정보와 파일 내용을 축력하는 예제
URL url = new URL("https://www.naver.com/index.html");
//Header 정보 가져오기
//URLConnection객체 구하기
URLConnection urlCon = url.openConnection();
System.out.println("Content-Type : "+urlCon.getContentType());
System.out.println("Encoding : "+urlCon.getContentEncoding());
System.out.println("Content : "+urlCon.getContent());
System.out.println();
//전체 Header 정보 출력하기
Map<String, List<String>> headerMap = urlCon.getHeaderFields();
//Header의 key값 구하기
Iterator<String> iterator = headerMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
System.out.println(key+" : "+headerMap.get(key));
}
System.out.println("====================================================");
//해당 호스트의 페이지 내용 가져오기
//파일을 읽어 오기 위한 스트림 객체 생성
//방법1 => URLconnection의 getInputStream()메서드 이용하기
//방법2 => URL객체의 openStream()메서드 이용하기
//InputStream is = url.openConnection().getInputStream();
InputStream is = url.openStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
int c;
while((c=isr.read() )!= -1){
System.out.print((char) c);
}
//스트림 닫기
isr.close();
}
}
'고급 JAVA' 카테고리의 다른 글
JAVA- TCP/IP - 멀티 채팅 프로그램 (0) | 2020.10.15 |
---|---|
JAVA -TCP/IP 통신 1:1 (0) | 2020.10.15 |
JAVA - Log4j (0) | 2020.10.08 |
JAVA - Singleton (0) | 2020.10.08 |
JAVA- MVC 회원관리 예제 (0) | 2020.10.07 |