?? xwebclient.java
字號:
/**《JXTA網(wǎng)絡(luò)編程》例程
*"第五章 JXTA深入編程" 即時消息軟件XWebClient
*/
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();
}
/**
* 實現(xiàn)InputPipe 的監(jiān)聽器
* @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;
}
/**
* 對網(wǎng)頁內(nèi)容進行處理
* @param mi 獲得的消息
*/
public void doWebContent(MessageImpl mi) {
System.out.println("捕獲一個網(wǎng)站內(nèi)容!");
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) {
}
}
/**
* 對網(wǎng)站進行第一次連接
*/
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 數(shù)據(jù)源流
* @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);
}
}
/**
* 實現(xiàn)接口HyperlinkListener
* @param event 超文本連接被激發(fā)時產(chǎn)生的事件
*/
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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -