import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SourceView extends JFrame implements ActionListener{
JPanel p = new JPanel();
JLabel urlLbl = new JLabel("URL : ");
JTextField tf = new JTextField(20);
JButton okBtn = new JButton("확인");
JScrollPane sp;
JTextArea ta = new JTextArea();
public SourceView() {
p.add(urlLbl); p.add(tf); p.add(okBtn);
add(p, "North");
sp = new JScrollPane(ta);
add(sp, "Center");
setSize(500, 500);
setVisible(true);
tf.addActionListener(this);
okBtn.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
Object obj = ae.getSource();
if(obj==tf || obj==okBtn){
if(tf.getText() != null){
try{
URL url = new URL(tf.getText());
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
//////
String contentType = conn.getContentType();
int idx = contentType.indexOf("charset=");
String unicode = contentType.substring(idx+8);
InputStreamReader isr = new InputStreamReader(is, unicode);
BufferedReader br = new BufferedReader(isr);
ta.setText("");
while(true){
String str = br.readLine();
if(str==null) break;
ta.append(str+"\n");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
public static void main(String[] args) {
new SourceView();
}
}
'응용 SoftWare > JAVA' 카테고리의 다른 글
Chat (0) | 2016.12.28 |
---|---|
SocketTest (0) | 2016.12.28 |
URL Test (0) | 2016.12.23 |
InetAddress Test (0) | 2016.12.23 |
Text Edit (0) | 2016.12.23 |