黑马程序员:Java基础总结----GUI&网络&IO综合开发

时间:2021-08-18 15:35:58
黑马程序员:Java基础总结


GUI&网络&IO综合开发
 ASP.Net+Android+IO开发.Net培训、期待与您交流!




网络架构
C/S:Client/Server
客户端,服务端。
特点:
1,需要在客户端和服务端都需要按照编写的软件。
2,维护较麻烦。
好处:可以减轻服务端的压力,如网络游戏。

B/S:Browser/Server
浏览器 ,服务端。
1,客户端不用单独编写软件。
     因为客户端用的就是浏览器。
2,对于软件升级,只要考虑服务端即可。
弊端:所有的程序都运行在服务端,客户端的浏览器毕竟解析能力较弱。对游戏等

类 URL

类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。构造: URL url = new URL("http://192.168.1.254/myweb/demo.html?name=haha&age=30")方法: String getFile()
          获取此 URL 的文件名。
String getHost()
          获取此 URL 的主机名(如果适用)。
String getPath()
          获取此 URL 的路径部分。
int getPort()
          获取此 URL 的端口号。
String getProtocol()
          获取此 URL 的协议名称。
String getQuery()
          获取此 URL 的查询部

类 URLConnection

抽象类 URLConnection 是所有类的超类,它代表应用程序和 URL 之间的通信链接。此类的实例可用于读取和写入此 URL 引用的资源。
URLConnection openConnection()
          返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
     URL url = new URL("http://192.168.1.254:8080/myweb/demo.html");

          URLConnection conn = url.openConnection();
          System.out.println(conn);
     
 InputStream getInputStream()
          返回从此打开的连接读取的输入流。
    
          InputStream in = conn.getInputStream();
          byte[] buf = new byte[1024];
          int len = in.read(buf);
          System.out.println(new String(buf,0,len));


演示客户端和服务端。

1,
客户端:浏览器 (telnet)
服务端:自定义。

2,
客户端:浏览器。
服务端:Tomcat服务器。

3,
客户端:自定义。(图形界面)
服务端:Tomcat服务器

import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;
public class Ts {       private Frame f ;       private TextField tf ;       private Button but ;       private TextArea ta ;
       private Dialog d ;       private Label lab ;       private Button okBut ;
      Ts() {            init();      }
       public void init() {             f = new Frame("my window" );             f.setBounds(300, 100, 600, 500);             f.setLayout( new FlowLayout());
             tf = new TextField(60);
             but = new Button("转到");
             ta = new TextArea(25, 70);
             d = new Dialog(f , "提示信息-self" , true);             d.setBounds(400, 200, 240, 150);             d.setLayout( new FlowLayout());             lab = new Label();             okBut = new Button("确定");
             d.add( lab);             d.add( okBut);
             f.add( tf);             f.add( but);             f.add( ta);
            myEvent();             f.setVisible( true);      }
       private void myEvent() {
             okBut.addActionListener(new ActionListener() {                   public void actionPerformed(ActionEvent e) {                         d.setVisible( false);                  }            });             d.addWindowListener( new WindowAdapter() {                   public void windowClosing(WindowEvent e) {                         d.setVisible( false);                  }            });
             tf.addKeyListener( new KeyAdapter() {                   public void keyPressed(KeyEvent e) {                         try {                               if (e.getKeyCode() == KeyEvent.VK_ENTER)                                    showDir();                        } catch (Exception ex) {                        }
                  }            });
             but.addActionListener(new ActionListener() {                   public void actionPerformed(ActionEvent e) {                         try {                              showDir();                        } catch (Exception ex) {                        }
                  }            });
             f.addWindowListener( new WindowAdapter() {                   public void windowClosing(WindowEvent e) {                        System. exit(0);                  }            });      }
       private void showDir() throws Exception {
             ta.setText( "");            String urlPath = tf.getText(); // http://192.168.1.254:8080/myweb/demo.html
            URL url = new URL(urlPath);
            URLConnection conn = url.openConnection();
            InputStream in = conn.getInputStream();
             byte[] buf = new byte[1024];
             int len = in.read(buf);
             ta.setText( new String(buf, 0, len));
      }
       public static void main(String[] args) {             new Ts();      }}




 ASP.Net+Android+IO开发.Net培训、期待与您交流!