網絡通訊三要素:IP地址,端口號,傳輸協議udp,tcp
ip地址:對象:InetAddress getByAddress()//獲取ip地址對象,包括主機名和地址值; getAllByName():給定主機名,返回ip地址組成的數組
練習:通過udp傳輸方式,將一段文字數據發送出去
步驟:1.建立udpsocket服務
2.提供數據,將數據封裝到數據包中
3.通過socket服務的發送功能,將數據包發送出去
4.關閉資源
import java.net.*;
class UdpSend
{
public static void main(String[] args)throws Exception
{
//創建udp服務,通過DatagramSocket對象
DatagramSocket ds=new DatagramSocket();
//確定數據,並封裝成數據包
byte[] buf="udp shuju bao".getBytes(); //將數據轉換成字節存入字節數組中
//創建包,包中包括要發送數據的長度,接收數據的ip地址名(地址值),端口
DatagramPacket dp=new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.28"),10000);
//通過socket服務,將已有的數據包發送出去
ds.send(dp);
//關閉資源
ds.close();
}
}
定義一個應用程序,用於接收udp協議傳輸的數據並處理
步驟:1.定義udpsocket服務,通常會監聽一個端口,其實就是給這個接收網絡應用程序定義數字標識,便於知道接收哪些數據該做出處理
2.定義一個數據包,存儲接收的數據。數據包對象中有更多功能可以提取字節數據中的不同數據信息
3.通過socket服務的接收功能,接收數據包中的數據並存入到已定義好的數據包中
4.根據數據包具有的特有功能,將這些不同數據取出,打印在控制檯上。
5.關閉資源
import java.net.*;
class UdpReceive
{
public static void main(String[] args) throws Exception
{
//創建udp socket,建立端點
DatagramSocket ds=new DatagramSocket(10000);
//定義數據包,用於存儲數據
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,buf.length);
//通過服務的receive方法將收到的數據存到包中
ds.receive(dp);
//通過數據包的方法獲取包中數據
String ip=dp.getAddress().getHostAddress(); //獲取地址
String data=new String(dp.getData(),0,dp.getLength()); //獲取數據,並將數組中的有效數據轉換爲字符串
int port =dp.getPort(); //獲取端口信息
System.out.println(ip+"::"+data+"::"+port);
//關閉資源
ds.close();
}
}
接收端先開等待接收數據:
發送端發送數據:
練習:獲取鍵盤錄入,然後發送端將錄入數據發送,接收端接收
發送端錄入:
接收端顯示:
編寫一個聊天程序:收發數據都可以同時操作,就需要引用多線程
import java.io.*;
import java.net.*;
class Send implements Runnable //創建發送線程
{
private DatagramSocket ds; //引用udp socket變量,因爲有收發兩個,所以可以引用成員變量,可以在後面初始化
public Send(DatagramSocket ds)
{
this.ds=ds; //將ds運用到整個類中
}
public void run()
{
try
{
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in)); //獲取鍵盤錄入,存入緩衝區
String line=null;
while((line=bufr.readLine())!=null) //開始讀取流中錄入的數據
{
if("886".equals(line))
break;
byte[] buf=line.getBytes(); //將數據轉入字節數組中
DatagramPacket dp= //創建包,將數據存入包中
new DatagramPacket(buf,0,buf.length,InetAddress.getByName("192.168.1.255"),10002); //用255段表示將數據以廣播的形式發送出去
ds.send(dp);
}
}
catch(Exception e)
{
throw new RuntimeException("發送失敗");
}
}
}
class Rece implements Runnable //創建接收線程
{
private DatagramSocket ds; //引用udp socket變量,因爲有收發兩個,所以可以引用成員變量,可以在後面初始化
public Rece(DatagramSocket ds)
{
this.ds=ds;
}
public void run()
{
try
{
while(true)
{
byte[] buf=new byte[1024]; //創建字節數組,便於存儲接收到的數據
DatagramPacket dp=new DatagramPacket(buf,buf.length); //創建包,便於存儲數據
ds.receive(dp); //將接受到的數據放入包中
String ip=dp.getAddress().getHostAddress(); //獲取接收數據的ip地址
String data=new String(dp.getData(),0,dp.getLength()); //獲取接收到的數據
System.out.println(ip+"::"+data);
}
}
catch(Exception e)
{
throw new RuntimeException("接收失敗");
}
}
}
class ChatDemo
{
public static void main(String[] args) throws Exception
{
//創建udpSocket服務
DatagramSocket sendSocket=new DatagramSocket();
DatagramSocket receSocket=new DatagramSocket(10002);
//開始線程運行
new Thread(new Send(sendSocket)).start();
new Thread(new Rece(receSocket)).start();
}
}
Tcp傳輸:tcp分爲客戶端和服務端,客戶端服務soket,服務端服務:serverSoket
演示Tcp傳輸
因爲Tcp是面向連接的,所以在建立socket服務時,就要有服務端存在,並連接成功,形成通路後,在該通道進行數據的傳輸
建立客戶端服務,發送數據
import java.net.*;
import java.io.*;
class TcpClient
{
public static void main(String[] args) throws Exception
{
//創建socket服務,指定目的主機和端口
Socket=s=new Socket("192.168.1.28",10005);
//獲取socket流中的輸出流,發送數據
OutputStream out=s.getOutputStream();
out.write("tcp ,我來了".getBytes());
s.close();
}
}
服務端:接收數據,打印在控制檯上
步驟:1.建立服務端的socket服務,並監聽一個端口
2.獲取連接過來的客戶端對象,通過ServerSocket的accept方法,沒有連接就會等待,所以這個方法是阻塞式的
3.客戶端如果發送過來數據,服務器端要使用對應的客戶端對象,並獲取到該對象的讀取流讀取發送過來的數據,打印控制檯上
4.關閉服務端(可以不關)
練習:演示tcp的客戶端和服務端的互訪
需求:客戶端給服務端發送數據,服務端接收到後,給客戶端反饋信息
客戶端步驟:1.建立socket服務,指定要連接的主機和端口
2.獲取socket流中的輸出流,將數據寫到流中,通過網絡發送給服務端
3.獲取socket流中的輸入流,將服務器返回的數據獲取到並打印
import java.io.*;
import java.net.*;
class TcpClient2
{
public static void main(String[] args) throws Exception
{
//創建socket服務
Socket s=new Socket("192.168.1.28",10004);
//獲取輸出流,方便寫入數據
OutputStream out=s.getOutputStream();
//寫入數據
out.write("服務端,你好".getBytes());
//獲取輸入流,讀取服務端返回的信息
InputStream in=s.getInputStream();
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}
}
class TcpServer2
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10004); //使用ServerSocket服務
Socket s=ss.accept(); //接收客戶端發送的數據
String ip=s.getInetAddress().getHostAddress(); //獲取客戶端的ip地址
InputStream in=s.getInputStream(); //獲取輸入流,便於讀取客戶端發送的數據
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
OutputStream out=s.getOutputStream(); //獲取輸出流,便於寫反饋信息給客戶端
out.write("收到了,你也好".getBytes());
s.close();
ss.close();
}
}
練習:建立一個文本轉換服務器,客戶端給服務端發送文本,服務端會將文本轉成大寫再返回客戶端。而且客戶端可以不斷的發送,直到發送“over”結束
分析:客戶端:源:鍵盤錄入; 目的:網絡設備,網絡輸出流
import java.io.*;
import java.net.*;
class TcpClie
{
public static void main(String[] args)throws Exception
{
//創建客戶端Socket服務
Socket s=new Socket("192.168.1.28",10006);
//獲取鍵盤錄入
BufferedReader bufw=new BufferedReader(new InputStreamReader(System.in));
//定義目的,將數據寫入socket流中,發給服務器
BufferedWriter bufout=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
//定義一個socket讀取流,讀取服務端返回的數據
BufferedReader bufin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=bufw.readLine())!=null)
{
if("over".equals(line))
break;
//pw.println(line);
bufout.write(line); //如果輸入的不是over,則將讀取的數據寫入流中
bufout.newLine(); //換行標誌,表示已經讀完了
bufout.flush(); //刷新緩衝區的數據,確保發送出去
String str=bufin.readLine(); //獲取服務端返回的數據
System.out.println(str);
}
bufw.close();
s.close();
}
}
class TcpServ
{
public static void main(String[] args)throws Exception
{
//創建ServerSocket服務
ServerSocket ss=new ServerSocket(10006);
Socket s=ss.accept(); //服務端接收客戶端連接對象
String ip=s.getInetAddress().getHostAddress(); //獲取客戶端地址
System.out.println(ip);
//讀取流中的數據
BufferedReader bufin=new BufferedReader(new InputStreamReader(s.getInputStream()));
//目的,將大寫數據寫入socket輸出流,返回給客戶端
BufferedWriter bufout=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
String line=null;
while((line=bufin.readLine())!=null)
{
//pw.println(line);
System.out.println(line);
bufout.write(line.toUpperCase()); //將數據轉成大寫
bufout.newLine(); //換行標誌,表示已經讀完了
bufout.flush(); //刷新緩衝區,將數據發送出去
}
s.close();
ss.close();
}
}
服務端:
客戶端:
tcp文件複製
import java.io.*;
import java.net.*;
class TextClient
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.28",11000); //使用Socket服務
BufferedReader bufr=new BufferedReader(new FileReader("IPDemo.java")); //源:.java文件
PrintWriter pw=new PrintWriter(s.getOutputStream(),true); //目的:socket服務的輸出流
String line=null;
while((line=bufr.readLine())!=null)
{
pw.println(line);
}
s.shutdownOutput(); //關閉客戶端的輸出流,相當於給流中加一個結束標記
//接收服務器返回的數據
BufferedReader bufin=new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=bufin.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
class TextServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(11000); //獲取ServerSocket服務
Socket s=ss.accept(); //接收客戶端傳來的連接對象
String ip=s.getInetAddress().getHostAddress(); //接收客戶端ip地址
System.out.println(ip);
BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream())); //服務器端的源:接受到socket源中的數據
PrintWriter pw=new PrintWriter(new FileWriter("serve.txt"),true); //目的,存入.txt文件中
String line=null;
while((line=bufr.readLine())!=null)
{
pw.println(line);
}
PrintWriter p=new PrintWriter(s.getOutputStream(),true); //返回客戶端數據
p.println("上傳成功");
pw.close();
s.close();
ss.close();
}
}
客戶端上傳,並收到反饋服務端接收
練習:上傳圖片
import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] arga) throws Exception
{
Socket s=new Socket("192.168.1.28",10007); //使用socket服務
FileInputStream fis=new FileInputStream("c:\\p1.jpg"); //讀取圖片文件
OutputStream out=s.getOutputStream(); //獲取socket服務的輸出流,方便數據上傳
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
s.shutdownOutput(); //關閉輸出流,給服務器標識數據已傳輸完成;
InputStream in=s.getInputStream(); //獲取socket服務的輸入流,便於接收服務端反饋信息
byte[] bufin=new byte[1024];
int num=in.read(bufin); //讀取反饋信息
System.out.println(new String(bufin,0,num));
fis.close();
s.close();
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10007); //獲取ServerSocket服務
Socket s=ss.accept(); //接收客戶端傳來的連接對象
String ip=s.getInetAddress().getHostAddress(); //獲取客戶端ip地址
System.out.println(ip);
InputStream in=s.getInputStream(); //獲取ServerSocket服務的輸入流,方便讀取數據
FileOutputStream fos=new FileOutputStream("serve.jpg"); //創建存儲接收數據的文件
int len=0;
byte[] buf=new byte[1024];
while((len=in.read(buf))!=-1) //讀取輸入流中的數據存入數組中
{
fos.write(buf,0,len); //將數組中的數據寫入文件中
}
OutputStream out=s.getOutputStream(); //獲取服務中的輸出流,能夠返回客戶端信息
out.write("上傳成功".getBytes());
fos.close();
s.close();
ss.close();
}
}
客戶端上傳,並獲取反饋信息
服務端接收數據
練習:多個客戶端可以同時連接到服務端,並上傳數據
服務端將每個客戶端封裝到一個單獨的線程中,就可以同時處理多個客戶的請求了
import java.io.*;
import java.net.*;
class PicThread implements Runnable
{
private Socket s;
PicThread(Socket s)
{
this.s=s;
}
public void run()
{
int count=1;
String ip=s.getInetAddress().getHostAddress(); //獲取客戶端ip地址
System.out.println(ip);
try
{
InputStream in=s.getInputStream(); //獲取ServerSocket服務的輸入流,方便讀取數據
File file=new File(ip+"("+count+")"+".jpg"); //多次接收,避免覆蓋
while(file.exists())
file=new File(ip+"("+(count++)+")"+".jpg"); //存在就創建一個新的
FileOutputStream fos=new FileOutputStream(file); //創建存儲接收數據的文件
int len=0;
byte[] buf=new byte[1024];
while((len=in.read(buf))!=-1) //讀取輸入流中的數據存入數組中
{
fos.write(buf,0,len); //將數組中的數據寫入文件中
}
OutputStream out=s.getOutputStream(); //獲取服務中的輸出流,能夠返回客戶端信息
out.write("上傳成功".getBytes());
fos.close();
s.close();
}
catch(Exception e)
{
System.out.println(ip+"上傳失敗");
}
}
}
class PicClient
{
public static void main(String[] args) throws Exception
{
if(args.length!=1) //判斷沒有文件,就接收一個文件
{
System.out.println("請選擇一個jpg格式的圖片");
return;
}
File file=new File(args[0]); //通過主函數傳入圖片文件
if(!(file.exists() && file.isFile()))
{
System.out.println("該文件要麼不存在,要麼不是圖片文件");
return;
}
if(!(file.getName().endsWith(".jpg")))
{
System.out.println("圖片格式錯誤,請重新選擇");
return;
}
if(file.length()>1024*1024*5)
{
System.out.println("文件過大,不予接收");
return;
}
Socket s=new Socket("192.168.1.198",10011); //使用socket服務
FileInputStream fis=new FileInputStream(file); //讀取圖片文件
OutputStream out=s.getOutputStream(); //獲取socket服務的輸出流,方便數據上傳
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
s.shutdownOutput(); //關閉輸出流,給服務器標識數據已傳輸完成;
InputStream in=s.getInputStream(); //獲取socket服務的輸入流,便於接收服務端反饋信息
byte[] bufin=new byte[1024];
int num=in.read(bufin); //讀取反饋信息
System.out.println(new String(bufin,0,num));
fis.close();
s.close();
}
}
class PicServer
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10011); //獲取ServerSocket服務
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start(); //創建上傳數據的線程
}
//ss.close();
}
}
練習:自定義一個服務器,客戶端是瀏覽器
import java.net.*;
import java.io.*;
class ServerDemo
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(13000); //引用ServerSocket服務,
Socket s=ss.accept(); //連接到客戶端
System.out.println(s.getInetAddress().getHostAddress()); //獲取客戶端地址
PrintWriter pw=new PrintWriter(s.getOutputStream(),true); //返回客戶端數據
pw.println("服務器你好");
s.close();
ss.close();
}
}
客戶端是服務器,服務端是TomCat服務器:啓動Tomcat服務器,在Tomcat文件夾的Webapps中新建MyWeb文件夾,裏面自己創建一個網頁,打開主機網頁,端口是8080
自定義一個客戶端,服務端還是TomCat服務器
打開Tomcat服務器,運行MyIE
import java.io.*;
import java.net.*;
class MyIE
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("192.168.1.198",8080);
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
//模擬瀏覽器發送的返回信息
out.println("GET /MyWeb/MyWeb.html HTTP/1.1");
out.println("Accept:*/*");
out.println("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
out.println("Host: 192.168.1.198:13000");
out.println("Connection: closed");
//用空行將頭部信息和請求體分隔開
out.println();
out.println();
BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=bufr.readLine())!=null)
{
System.out.println(line);
}
s.close();
}
}
/*
D:\java0217\day24>java ServerDemo
192.168.1.198
GET / HTTP/1.1
Host: 192.168.1.198:13000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Geck
35.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0
*/
在命令行顯示出了網頁內容
下面利用圖形化界面來實現顯示自定義網頁內容
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIEGUI
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d; //引用一個對話框成員變量
private Label lab; //引用一個標籤
private Button okbu; //引用按鈕變量,應用於對話框中
MyIEGUI()
{
init();
}
public void init()
{
f=new Frame("my window"); //創建一個窗體
f.setBounds(300,100,600,500); //設置窗體的位置,寬高
f.setLayout(new FlowLayout()); //設置成流佈局
tf=new TextField(60); //新建文本框,設置列數是30
but=new Button("轉到"); //新建按鈕
ta=new TextArea(25,70); //新建文本編輯區域,15行40列
d=new Dialog(f,"提示信息-self",true); //新建一個對話框,true值表示當前窗口不處理就無法處理後置窗口
d.setBounds(400,200,240,150); //設置對話框的位置和寬高
d.setLayout(new BorderLayout()); //設置對話框佈局爲流式佈局
lab=new Label(); //創建標籤
okbu=new Button("確定"); //創建對話框中的按鈕
d.add(lab);
d.add(okbu);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true); //設置窗口可視
}
private void myEvent()
{
okbu.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)
{
throw new RuntimeException();
}
}
});
but.addActionListener(new ActionListener() //添加窗體監聽器
{
public void actionPerformed(ActionEvent e) //操作時調用
{
try
{
showDir(); //在操作時調用目錄轉入文本區功能函數
}
catch(Exception ex)
{
throw new RuntimeException();
}
}
});
f.addWindowListener(new WindowAdapter() //添加窗體關閉方法,複寫關閉方法
{
public void windowClosing(WindowEvent ex)
{
System.exit(0); //退出窗體
}
});
}
private void showDir() throws Exception //輸入目錄後轉入文本區的功能封裝成一個方法
{
ta.setText("");
String url=tf.getText(); //獲取網頁地址
int index1=url.indexOf("//")+2; //獲取地址角標
int index2=url.indexOf("/",index1); //獲取目錄角標
String str=url.substring(index1,index2); //獲取到ip地址和端口號
String[] arr=str.split(":");
String host=arr[0]; //得到主機地址
int port =Integer.parseInt(arr[1]); //得到端口號
String Path=url.substring(index2); //得到目錄列表
//ta.setText(str+"---"+Path);
Socket s=new Socket(host,port);
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
//模擬瀏覽器發送的返回信息
out.println("GET "+Path+" HTTP/1.1");
out.println("Accept:*/*");
out.println("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
out.println("Host: 192.168.1.198:13000");
out.println("Connection: closed");
//用空行將頭部信息和請求體分隔開
out.println();
out.println();
BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
s.close();
}
public static void main(String[] args)
{
new MyIEGUI();
}
}
運用url對象,解析頭字段
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIEGUI2
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d; //引用一個對話框成員變量
private Label lab; //引用一個標籤
private Button okbu; //引用按鈕變量,應用於對話框中
MyIEGUI2()
{
init();
}
public void init()
{
f=new Frame("my window"); //創建一個窗體
f.setBounds(300,100,600,500); //設置窗體的位置,寬高
f.setLayout(new FlowLayout()); //設置成流佈局
tf=new TextField(60); //新建文本框,設置列數是30
but=new Button("轉到"); //新建按鈕
ta=new TextArea(25,70); //新建文本編輯區域,15行40列
d=new Dialog(f,"提示信息-self",true); //新建一個對話框,true值表示當前窗口不處理就無法處理後置窗口
d.setBounds(400,200,240,150); //設置對話框的位置和寬高
d.setLayout(new BorderLayout()); //設置對話框佈局爲流式佈局
lab=new Label(); //創建標籤
okbu=new Button("確定"); //創建對話框中的按鈕
d.add(lab);
d.add(okbu);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true); //設置窗口可視
}
private void myEvent()
{
okbu.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)
{
throw new RuntimeException();
}
}
});
but.addActionListener(new ActionListener() //添加窗體監聽器
{
public void actionPerformed(ActionEvent e) //操作時調用
{
try
{
showDir(); //在操作時調用目錄轉入文本區功能函數
}
catch(Exception ex)
{
throw new RuntimeException();
}
}
});
f.addWindowListener(new WindowAdapter() //添加窗體關閉方法,複寫關閉方法
{
public void windowClosing(WindowEvent ex)
{
System.exit(0); //退出窗體
}
});
}
private void showDir() throws Exception //輸入目錄後轉入文本區的功能封裝成一個方法
{
ta.setText("");
String urlpath=tf.getText(); //獲取網頁地址
URL url=new URL(urlpath); //將網頁地址傳入資源定位符中
URLConnection conn=url.openConnection(); //返回url連接到的遠程對象
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 MyIEGUI2();
}
}