일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
- write by chatGPT
- 인프라
- 자바암호
- 데이터베이스
- flet
- 파이썬
- GPT-4's answer
- 자바네트워크
- android
- jpa
- python
- write by GPT-4
- JVM
- 유닉스
- spring integration
- 고전역학
- 역학
- 소프트웨어공학
- 리눅스
- 자바
- spring data jpa
- 코틀린
- Java
- 시스템
- NIO
- oracle
- kotlin
- Database
- 웹 크롤링
- chatGPT's answer
- Today
- Total
기억을 지배하는 기록
Java 네트워크 - 2 본문
4. 인터넷 주소 처리
InetAddress Class
① package network.net; ② import java.net.*; ③ ④ public class InetAddressMain { ⑤ public InetAddressMain() { ⑥ } ⑦ ⑧ private void exec(String[] Domain) { ⑨ try { ⑩ for( int leng = 0; leng < Domain.length; leng++) { ⑪ InetAddress inetaddress[] = InetAddress.getAllByName(Domain[leng]); ⑫ for(int i = 0; i < inetaddress.length; i++) { ⑬ String hostName = inetaddress[i].getHostAddress(); ⑭ System.out.println(hostName); ⑮ } 16 } 17 } catch (UnknownHostException unhoste) { 18 System.err.println(unhoste); 19 } 20 21 } 22 public static void main(String[] args) { 23 InetAddressMain inetAddressMain = new InetAddressMain(); 24 if(args.length > 0) { 25 System.out.println("Use : java InetAddressMain [Domain 1] [Domain 2] ..."); 26 System.exit(-1); 27 } 28 inetAddressMain.exec(args); 29 } 30 } |
5. URL클래스를 이용해 데이터를 가져오기
① // network.net.httpMgr ② ③ package network.net; ④ import java.awt.Frame; ⑤ import java.awt.event.WindowAdapter; ⑥ import java.awt.event.WindowEvent; ⑦ import javax.swing.UIManager; ⑧ import java.awt.Dimension; ⑨ import java.awt.Toolkit; ⑩ ⑪ public class httpMgr { ⑫ public httpMgr() { ⑬ Frame frame = new httpMgrFrame(); ⑭ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); ⑮ Dimension frameSize = frame.getSize(); 16 if (frameSize.height > screenSize.height) { 17 frameSize.height = screenSize.height; 18 } 19 if (frameSize.width > screenSize.width) { 20 frameSize.width = screenSize.width; 21 } 22 frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 23 frame.addWindowListener(new WindowAdapter() { 24 public void windowClosing(WindowEvent e) { 25 System.exit(0); 26 } 27 }); 28 frame.setVisible(true); 29 } 30 31 public static void main(String[] args) { 32 try { 33 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 34 } catch(Exception e) { 35 e.printStackTrace(); 36 } 37 new httpMgr(); 38 } 39 } |
① // network.net.httpMgrFrame ② ③ package network.net; ④ import javax.swing.JFrame; ⑤ import java.awt.Dimension; ⑥ import javax.swing.JTextField; ⑦ import java.awt.Rectangle; ⑧ import javax.swing.BorderFactory; ⑨ import javax.swing.JButton; ⑩ import javax.swing.JTextArea; ⑪ import javax.swing.JScrollPane; ⑫ import javax.swing.JTextPane; ⑬ import java.awt.event.ActionListener; ⑭ import java.awt.event.ActionEvent; ⑮ import java.net.*; 16 import java.io.*; 17 18 public class httpMgrFrame extends JFrame { 19 private JTextField Ip = new JTextField(); 20 private JTextField Port = new JTextField(); 21 private JTextField Uri = new JTextField(); 22 private JTextField fileName = new JTextField(); 23 24 private JButton submit = new JButton(); 25 private JButton reset = new JButton(); 26 private JButton saveAs = new JButton(); 27 28 private JTextArea property = new JTextArea(); 29 private JScrollPane jScrollPane1 = new JScrollPane(); 30 private JTextPane htmlMessage = new JTextPane(); 31 32 public httpMgrFrame() { 33 try { 34 jbInit(); 35 } catch(Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 40 private void jbInit() throws Exception { 41 this.getContentPane().setLayout(null); 42 this.setSize(new Dimension(512, 602)); 43 this.setTitle("HTTP MGR"); 44 Ip.setBounds(new Rectangle(35, 25, 150, 45)); 45 Ip.setBorder(BorderFactory.createTitledBorder("IP")); 46 Ip.setNextFocusableComponent(Port); 47 48 Port.setBounds(new Rectangle(35, 80, 150, 45)); 49 Port.setBorder(BorderFactory.createTitledBorder("PORT")); 50 Port.setNextFocusableComponent(Uri); 51 52 Uri.setBounds(new Rectangle(35, 135, 150, 45)); 53 Uri.setBorder(BorderFactory.createTitledBorder("URI")); 54 Uri.setNextFocusableComponent(property); 55 56 submit.setText("submit"); 57 submit.setBounds(new Rectangle(220, 85, 85, 35)); 58 submit.setActionCommand("submit"); 59 submit.setNextFocusableComponent(reset); 60 submit.addActionListener(new ActionListener() { 61 public void actionPerformed(ActionEvent e) { 62 submit_actionPerformed(e); 63 } 64 }); 65 66 reset.setText("reset"); 67 reset.setBounds(new Rectangle(350, 85, 85, 35)); 68 reset.addActionListener(new ActionListener() { 69 public void actionPerformed(ActionEvent e) { 70 reset_actionPerformed(e); 71 } 72 }); 73 74 property.setBounds(new Rectangle(35, 190, 435, 55)); 75 property.setBorder(BorderFactory.createTitledBorder("PROPERTY")); 76 property.setNextFocusableComponent(submit); 77 78 jScrollPane1.setBounds(new Rectangle(40, 275, 430, 180)); 79 htmlMessage.setBorder(BorderFactory.createTitledBorder("HTML MESSAGE")); 80 fileName.setBounds(new Rectangle(40, 485, 275, 45)); 81 fileName.setBorder(BorderFactory.createTitledBorder("FILE NAME")); 82 fileName.setNextFocusableComponent(saveAs); 83 fileName.setEnabled(false); 84 saveAs.setText("save as"); 85 saveAs.setBounds(new Rectangle(360, 490, 85, 35)); 86 saveAs.setActionCommand("saveAs"); 87 saveAs.setEnabled(false); 88 saveAs.addActionListener(new ActionListener() { 89 public void actionPerformed(ActionEvent e) { 90 saveAs_actionPerformed(e); 91 } 92 }); 93 jScrollPane1.getViewport().add(htmlMessage, null); 94 95 this.getContentPane().add(saveAs, null); 96 this.getContentPane().add(fileName, null); 97 this.getContentPane().add(jScrollPane1, null); 98 this.getContentPane().add(property, null); 99 this.getContentPane().add(reset, null); 100 this.getContentPane().add(submit, null); 101 this.getContentPane().add(Uri, null); 102 this.getContentPane().add(Port, null); 103 this.getContentPane().add(Ip, null); 104 } 105 106 private void reset_actionPerformed(ActionEvent e) { 107 // 아직 구현 안함. 108 } 109 110 private void submit_actionPerformed(ActionEvent e) { 111 String ipString = Ip.getText(); 112 int portNum = Integer.parseInt(Port.getText()); 113 String uriString = Uri.getText(); 114 String propertyString = property.getText(); 115 try { 116 URL url = new URL("http",ipString,portNum,uriString); 117 StringBuffer sb = StreamParser.Parser(url.openStream()); 118 htmlMessage.setText(sb.toString()); 119 fileName.setEnabled(true); 120 saveAs.setEnabled(true); 121 } catch (MalformedURLException mfurle) { 122 htmlMessage.setText(mfurle.getMessage()); 123 } catch (IOException ioe) { 124 htmlMessage.setText(ioe.getMessage()); 125 } 126 } 127 128 private void saveAs_actionPerformed(ActionEvent e) { 129 try{ 130 FileWriter fileOut = new FileWriter(fileName.getText()); 131 fileOut.write(htmlMessage.getText()); 132 fileOut.close(); 133 htmlMessage.setText("파일이 저장되었습니다."); 134 } catch (IOException ioe) { 135 htmlMessage.setText("파일저장중 에러가 발생하였습니다."); 136 } 137 } 138 } 139 140 class StreamParser { 141 public static StringBuffer Parser(InputStream in) throws IOException { 142 StringBuffer sb = new StringBuffer(); 143 BufferedReader br = new BufferedReader(new InputStreamReader(in)); 144 String readString = ""; 145 while( (readString = br.readLine()) != null) { 146 sb.append(readString+" \n"); 147 } 148 return sb; 149 } 150 } |
'오래된글 > Java' 카테고리의 다른 글
Java 네트워크 - 4 (0) | 2018.04.09 |
---|---|
Java 네트워크 - 3 (0) | 2018.04.09 |
Java 네트워크 - 1 (0) | 2018.04.09 |
Jakarta Commons Logging (0) | 2018.04.07 |
Jakarta Common Configuration (0) | 2018.04.07 |