java 显示网页中特定的超链接Word格式文档下载.docx
- 文档编号:16314554
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:21
- 大小:19.26KB
java 显示网页中特定的超链接Word格式文档下载.docx
《java 显示网页中特定的超链接Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《java 显示网页中特定的超链接Word格式文档下载.docx(21页珍藏版)》请在冰豆网上搜索。
editPane=【代码1】//创建editPane
【代码2】//editPane设置为不可编辑状态
button=newJButton("
确定"
);
button.addActionListener(this);
thread=newThread(this);
p=newJPanel();
p.add(newJLabel("
输入网址:
"
));
p.add(text);
p.add(button);
con=getContentPane();
con.add(newJScrollPane(editPane),BorderLayout.CENTER);
con.add(p,BorderLayout.NORTH);
setBounds(60,60,450,300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicvoidactionPerformed(ActionEvente)
if(!
(thread.isAlive()))
try{
thread.start();
catch(Exceptionee)
{
text.setText("
我正在读取"
+url);
publicvoidrun()
try{
intm=-1;
editPane.setText(null);
url=【代码3】//使用构造方法URL(Strings)创建url,其中参数s由text中的文本指定
InputStreamin=【代码4】//url返回输入流
Filefile=newFile("
temp.html"
ByteArrayOutputStreamwrite=newByteArrayOutputStream();
while((m=in.read(b))!
=-1)
write.write(b,0,m);
write.close();
in.close();
bytecontent[]=write.toByteArray();
Stringstr=newString(content);
Patternpattern;
Matchermatch;
pattern=Ppile("
<
aHREF=http:
//.*>
*.育.*<
/a>
Pattern.CASE_INSENSITIVE);
match=pattern.matcher(str);
FileOutputStreamout=newFileOutputStream(file);
while(match.find())
StringstrHREF=match.group();
strHREF="
BR>
+strHREF;
out.write(strHREF.getBytes());
out.close();
newURL=file.toURL();
con.removeAll();
editPane=【代码5】//创建editPane
【代码6】//editPane设置为不可编辑状态
【代码7】//editPane显示newURL
con.validate();
editPane.addHyperlinkListener(newHyperlinkListener()
publicvoidhyperlinkUpdate(HyperlinkEvente)
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
URLlinkURL=e.getURL();
【代码8】//editPane显示linkURL
}
catch(IOExceptione1)
editPane.setText("
+e1);
);
catch(MalformedURLExceptione1)
return;
}
publicclassURLExample
publicstaticvoidmain(Stringargs[])
newWin();
11.2.2过滤网页中的内容
Example.java
editPane=【代码1】//创建editPane
setBounds(60,60,460,380);
IMG.*>
str=match.replaceAll("
bytecc[]=str.getBytes();
ByteArrayInputStreaminByte=newByteArrayInputStream(cc);
bytedd[]=newbyte[1024];
while((m=inByte.read(dd,0,1024))!
out.write(dd,0,m);
inByte.close();
publicclassExample
11.2.3使用套接字传输数据
按模板要求,将【代码1】~【代码16】替换为Java程序代码。
客户端模板:
Client.java
classClientextendsJFrameimplementsRunnable,ActionListener
JButtonconnection,computer;
JTextFieldinputA,inputB,inputC;
JTextAreashowResult;
Socketsocket=null;
DataInputStreamin=null;
DataOutputStreamout=null;
publicClient()
socket=newSocket();
connection=newJButton("
连接服务器"
computer=newJButton("
求三角形面积"
computer.setEnabled(false);
inputA=newJTextField("
0"
12);
inputB=newJTextField("
inputC=newJTextField("
BoxboxV1=Box.createVerticalBox();
boxV1.add(newJLabel("
输入边A"
输入边B"
输入边C"
BoxboxV2=Box.createVerticalBox();
boxV2.add(inputA);
boxV2.add(inputB);
boxV2.add(inputC);
BoxbaseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(boxV2);
Containercon=getContentPane();
con.setLayout(newFlowLayout());
showResult=newJTextArea(8,18);
con.add(connection);
con.add(baseBox);
con.add(computer);
con.add(newJScrollPane(showResult));
computer.addActionListener(this);
connection.addActionListener(this);
thread=newThread(this);
setBounds(100,100,360,310);
while(true)
try{
doublearea=【代码1】//in读入一个double型数据
showResult.append("
\n三角形的面积:
\n"
+area);
showResult.setCaretPosition((showResult.getText()).length());
catch(IOExceptione)
showResult.setText("
与服务器已断开"
break;
if(e.getSource()==connection)
try
if(socket.isConnected())
else
InetAddressaddress=InetAddress.getByName("
127.0.0.1"
InetSocketAddresssocketAddress=【代码2】
//创建端口为4331、地址为address的socketAddress
【代码3】//socket建立和socketAddress的连接呼叫
in=newDataInputStream(【代码4】);
//socket返回输入流
out=newDataOutputStream(【代码5】);
//socket返回输出流
computer.setEnabled(true);
catch(IOExceptionee){}
if(e.getSource()==computer)
doublea=Double.parseDouble(inputA.getText()),
b=Double.parseDouble(inputB.getText()),
c=Double.parseDouble(inputC.getText());
if(a+b>
c&
&
a+c>
b&
b+c>
a)
【代码6】//out写入a,发送到服务器
【代码7】//out写入b,发送到服务器
【代码8】//out写入c,发送到服务器
inputA.setText("
你输入的3个数不构成三角形"
请输入数字字符"
Clientwin=newClient();
服务器端模板:
Server.java
publicclassServer
ServerSocketserver=null;
ServerThreadthread;
Socketyou=null;
while(true)
server=【代码9】//创建在端口4331上负责监听的ServerSocket对象
catch(IOExceptione1)
System.out.println("
正在监听"
you=【代码10】//server返回和客户端相连接的Socket对象
客户的地址:
+you.getInetAddress());
catch(IOExceptione)
正在等待客户"
if(you!
=null)
newServerThread(you).start();
else
continue;
classServerThreadextendsThread
Socketsocket;
Strings=null;
ServerThread(Sockett)
socket=t;
in=newDataInputStream(【代码11】);
out=newDataOutputStream(【代码12】);
{}
publicvoidrun()
doublea=0,b=0,c=0,area=0;
a=【代码13】//in读入一个double型数据
b=【代码14】//in读入一个double型数据
c=【代码15】//in读入一个double型数据
doublep=(a+b+c)/2.0;
area=Math.sqrt(p*(p-a)*(p-b)*(p-c));
【代码16】//out写入area,发送到客户端
catch(IOExceptione)
客户离开"
11.2.4基于UDP的图像传输
按模板要求,将【代码1】~【代码12】替换为Java程序代码。
JLabellabel=newJLabel();
JButtonb=newJButton("
获取图像"
Containercon;
Client()
super("
Iamac
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 显示网页中特定的超链接 显示 网页 特定 超链接
![提示](https://static.bdocx.com/images/bang_tan.gif)