J2SE 网络通信(一)

用Swing框架创建窗体,为按钮添加点击事件的响应方法,在相应方法内调用获取网路资源大小,以及网页内容的方法。

简单的获取URL资源测试

用Swing框架创建窗体,为按钮添加点击事件的响应方法,在相应方法内调用获取网路资源大小,以及网页内容的方法。

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.j2se.network;

import java.awt.Color;
import java.awt.Font;
import java.awt.TextArea;
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 java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class GetNetResourceSize extends JFrame {

private JTextField textField_size;
private JTextField textField_url;
private TextArea textField_page;
//声明两个文本框控件,分别用于放置链接和资源的尺寸

public static void main(String args[]){
//用异常捕获结构放置创建窗体时出错
try{
GetNetResourceSize frame = new GetNetResourceSize();
//声明一个窗体对象
frame.setVisible(true);
//窗体设为可见
}catch(Exception e){
e.printStackTrace();
}
}

public GetNetResourceSize(){
// 自定义的窗体构造方法,继承了JFrame
super();
//继承父类

setTitle("Get the size of resource from Internet");
//设定标题栏的内容
getContentPane().setLayout(null);
//设置布局为空
setBounds(200,200,400,200);
//设置窗体位置以及大小

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗体关闭时的默认操作为“关闭”

final JLabel label_title = new JLabel();
//新建一个标签,用于显示标题

label_title.setForeground(Color.green); //设置标题的前景色(字体的颜色)
label_title.setFont(new Font("",Font.BOLD,20)); //设置字体为加粗 20号
label_title.setText("Get the size of resource from Internet"); //设置标签内要显示的文本
label_title.setBounds(5, 5, 400, 50); //设置标签的位置及大小

final JLabel label_title_url = new JLabel();
label_title_url.setText("Enter URL:");
label_title_url.setBounds(10,80,80,25);

final JLabel label_title_size = new JLabel();
label_title_size.setText("Size:");
label_title_size.setBounds(10,125,125,25);
//同上,设置了两个标签显示文本框的标题

textField_url = new JTextField();
textField_url.setBounds(80,80,286,25);
//实例化之前声明的文本框对象,并设置其位置和大小

textField_size = new JTextField();
textField_size.setBounds(80, 125, 145, 25);

textField_page = new TextArea();
textField_page.setBounds(10, 160, 380, 220);

final JButton button = new JButton();
//声明并实例化了一个按钮,用于执行获取资源大小的操作

button.setText("Get the size");
//设置按钮显示的文字
button.setBounds(235, 125, 135, 25);
//设置按钮的位置和大小
button.addActionListener(new ActionListener(){
//为按钮添加事件响应,用匿名内部类
public void actionPerformed(final ActionEvent e){
String url = textField_url.getText().trim();
//从文本框textField_url获取用户输入网址,调用trim()方法过滤前后的空格
try{
long len = resourceSize(url);
//调用自定义的resourceSize()方法获取网络资源大小
textField_size.setText(String.valueOf(len)+"byte(s)");
//textField_size中显示获取到的网络资源大小
Collection urlCollection = getURLCollection(url);
//调用自定义的方法getURLCollection(),传入url,获得网页内容的集合
Iterator it = urlCollection.iterator();
//获得这个集合对象的迭代器对象
while(it.hasNext()){
textField_page.setText(textField_page.getText()+(String)it.next()+"\n");
//.append((String)it.next()+"\n");
//在文本区域中显示解析到的网页内容
}
}catch(Exception ex){
ex.printStackTrace();
}


}
});

getContentPane().add(label_title);
getContentPane().add(label_title_url);
getContentPane().add(label_title_size);
getContentPane().add(textField_url);
getContentPane().add(textField_size);
getContentPane().add(button);
getContentPane().add(textField_page);
//将上述所有自定义的控件添加到父容器中
}

public Collection<String> getURLCollection(String urlString) {
URL url = null;
//声明URL对象
URLConnection conn = null;
//声明URLConnection通信链接对象
Collection<String> urlConnection = new ArrayList<String>();
//创建基于ArralList的集合对象,元素为String类型
try {
url = new URL(urlString);
//创建URL对象
conn = url.openConnection();
//打开连接 获得连接对象
conn.connect();
//连接到url引用资源,建立通信链接
InputStream is = conn.getInputStream();
//从连接中获取流对象
InputStreamReader in = new InputStreamReader(is,"UTF-8");
//将流对象转换为字符流
BufferedReader br = new BufferedReader(in);
//将字符流转换为缓冲流
String nextLine = br.readLine();
//从缓冲流中读取信息,解析网页内容
while(nextLine != null){
urlConnection.add(nextLine);
//解析网页的全部内容,并将其添加到集合中
nextLine = br.readLine();
//循环的从缓冲流中读取信息,解析网页
}

} catch (Exception e) {
e.printStackTrace();
}

return urlConnection;
//返回集合对象
}

public long resourceSize(String surl) throws Exception {

URL url = new URL(surl); //创建url对象
URLConnection urlconn = url.openConnection(); //获取网络连接对象
urlconn.connect(); //连接到url指定的资源
return urlconn.getContentLength(); //调用getContentlength()方法,得到资源大小并返回,单位为字节(long)
}

}

运行截图

network

如果觉得对您有帮助,就扫我交个朋友吧!