본문 바로가기

JAVA

자바채팅 프로그래밍 - 호스트 파일 정보 출력 , 객체구분

 

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class ReadServerFile extends Frame implements ActionListener {
	private TextField enter;
	private TextArea contents1, contents2;
	public ReadServerFile() {
		super("호스트 파일 읽기");
		setLayout(new BorderLayout());
		enter = new TextField("URL를 입력하세요!");
		enter.addActionListener(this);
		add(enter,BorderLayout.NORTH);
		contents1 = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
		contents2 = new TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
		add(contents1,BorderLayout.CENTER);
		add(contents2,BorderLayout.SOUTH);
		addWindowListener(new WinListener());
		setSize(350,500);
		setVisible(true);
	}
	public void actionPerformed( ActionEvent e) {
		URL url;
		InputStream is;
		BufferedReader input;
		String line;
		StringBuffer buffer1 = new StringBuffer();
		
		String location = e.getActionCommand();
		try {
			url = new URL(location);
			
			
			buffer1.append("원격 호스트의 프로토콜은 : "+ url.getProtocol()).append('\n');
			buffer1.append("원격 호스트의 호스트 이름는 : "+url.getHost()).append('\n');
			buffer1.append("원격 호스트의 포트 번호는 : "+ url.getPort()).append('\n');
			buffer1.append("원격 호스트의 파일이름는 : "+ url.getFile()).append('\n');
			buffer1.append("원격 호스트의 path는" + url.getPath()).append('\n');
			buffer1.append("원격 호스트의 ref는" + url.getRef()).append('\n');
			buffer1.append("원격 호스트의 해쉬코드는 : "+ url.hashCode()).append('\n');
			contents1.setText(buffer1.toString());
			
			/*
			 * 비디오와 오디오는 getContent()메소드로 형태를 알기가 힘들고 이로 읽어드릴 경우 텍스트로 읽혀 InputStream에서 걸려버
			 * 린다. 그래서 이를 방지하기 위해 예제와 다르게 if의 순서를 바꾸어 주었고getContentType()메소드를 사용하여 따로 식별하여 받아주었다. 
			 */
			URLConnection urlcon = url.openConnection();
			String contentType = urlcon.getContentType();
			Object o = url.getContent();
			StringBuffer buffer2 = new StringBuffer();
			//비디오일때
			if(contentType.equals("video/mp4")){
				contents2.setText("파일을 읽는 중입니다...");
				contents2.setText("이 파일은 비디오 입니다\n");
			}
			//오디오일때
			else if(contentType.equals("audio/mpeg")){
				contents2.setText("파일을 읽는 중입니다...");
				contents2.setText("이 파일은 오디오 입니다\n");
			//텍스트일때	
			}else if(o.getClass().getName().contains("InputStream")){
				is = url.openStream();
				input = new BufferedReader(new InputStreamReader(is));
				contents2.setText("파일을 읽는 중입니다...");
				while((line = input.readLine())!=null) {
						buffer2.append(line).append('\n');
						contents2.setText(buffer2.toString());
				}
				input.close();
			//이미지일때
			}else if(o.getClass().getName().contains("Image")){
				contents2.setText("파일을 읽는 중입니다...");
				contents2.setText("이 파일은 이미지 파일 입니다\n");
			}		
		}catch(MalformedURLException mal) {
			contents1.setText("URL 형식이 잘못되었습니다.");
		}catch(IOException io) {
		contents1.setText(io.toString());
		}catch(Exception ex) {
		contents1.setText("호스트 컴퓨터의 파일만을 열 수 있습니다.");
		}
	}
	public static void main(String args[]) {
		ReadServerFile read = new ReadServerFile();
	}
	class WinListener extends WindowAdapter{
		public void windowClosing(WindowEvent we) {
			System.exit(0);
		}
	}
}

 

<결과>

-비디오-

-오디오-

-텍스트-

-이미지-