📄 xwebclient.java
字号:
/**《JXTA网络编程》例程
*"第五章 JXTA深入编程" 即时消息软件XWebClient
*
@作者 慈黎利
@单位 清华大学计算机系软件所知识工程组
@版本 1.0
@联系方式 cili@163.com
@描述 浏览P2P中的Web网站
Copyright 2002
*/
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import net.jxta.pipe.*;
import net.jxta.impl.endpoint.*;
import demo.p2psocket.*;
//源程序 XWebClient.java
public class XWebClient implements PipeMsgListener,HyperlinkListener {
P2PSocket pps=new P2PSocket();
BaseMessage bm=new BaseMessage();
JEditorPane pane=new JEditorPane();
JFrame jf=new JFrame("P2PWeb浏览器!");
public XWebClient() {
pps.setInputPipeName("002s");
pps.setInListener(this);
pps.bind();
pps.setOutputPipeName("001s");
pps.setOutListener(pps);
pps.connect();
pane.addHyperlinkListener(this);
pane.setEditable(false);
jf.getContentPane().add(pane);
jf.setSize(500,500);
jf.show();
}
/**
* 实现InputPipe 的监听器
* @param event 获得消息事件
*/
public void pipeMsgEvent ( PipeMsgEvent event ){
String temp=null;
MessageImpl mi=(MessageImpl) event.getMessage();
temp=mi.getString(bm.WEB_MESSAGE_TYPE);
if (temp.equals(bm.WEB_CONTENT))
doWebContent(mi);
return;
}
/**
* 对网页内容进行处理
* @param mi 获得的消息
*/
public void doWebContent(MessageImpl mi) {
System.out.println("捕获一个网站内容!");
MessageElementImpl mei=(MessageElementImpl)mi.getElement(BaseMessage.WEB_CONTENT);
this.save2File(mei.getStream(),"temp.html");
try {
String userpath=System.getProperty("user.dir");
pane.setPage("file:///"+userpath);
pane.setPage("file:///"+userpath+"/temp.html");
}catch (Exception e) {
}
}
/**
* 对网站进行第一次连接
*/
public void connectWebFirst() {
pps.send(bm.newWEBMessage(this.file2Stream("adv/002s.xml"),null,null,
bm.WEB_REQUEST_FIRST,"002s"));
}
/**
* 把文件读取到流中
* @param file 将要读入的文件
* @return 返回包含文件信息的流
*/
public InputStream file2Stream(String file) {
try {
FileInputStream fis=new FileInputStream(file);
return fis;
}catch (FileNotFoundException fnfe) {
System.out.println("无法读取本地文件");
System.exit(-1);
}
return null;
}
/**
* 把流中的信息写道文件中
* @param is 数据源流
* @param fname 将要写入的文件
*/
public void save2File(InputStream is,String fname) {
int length;
byte buf[]=new byte[1024];
try {
FileOutputStream fos=new FileOutputStream(fname);
BufferedOutputStream bos=new BufferedOutputStream(fos);
BufferedInputStream bis=new BufferedInputStream(is);
while ((length=bis.read(buf,0,1024))!=-1) {
bos.write(buf,0,length);
bos.flush();
}
bis.close();
bos.close();
fos.close();
}catch (IOException ioe) {
System.out.println("写入文件错误:"+ioe);
}
}
/**
* 实现接口HyperlinkListener
* @param event 超文本连接被激发时产生的事件
*/
public void hyperlinkUpdate(HyperlinkEvent event) {
if (event.getEventType()== HyperlinkEvent.EventType.ACTIVATED){
try{
String url=event.getURL().toString();
String userpath="file:///"+System.getProperty("user.dir");
String temp=url.substring(userpath.length()-1);
pps.send(bm.newWEBMessage(null,null,null,bm.WEB_REQUEST,temp));
}catch(Exception e){
System.exit(-1);
}
}
}
public static void main(String args[]) {
XWebClient xwc=new XWebClient();
xwc.connectWebFirst();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -