困扰了我快半年的问题终于在春节当天解决,散分庆祝!
曾在
http://community.csdn.net/Expert/topic/5341/5341889.xml?temp=.3290979
上留过一个问题,花了我100分,最终还是没有头绪,问题如下:
网页动态屏蔽:
假设你现在在看我的帖子,看到的是屏蔽前的网页,但是,当运行网页动态屏蔽的java程序后,你所看到的却是
主 题: 高手请问:谁知道网页动态屏蔽的原理?
*** ***: java_road () Blog
等 级:
信 誉 值: 100
所属社区: Java J2SE / 基础类
问题****: 100
***次数: 4
发表***: 2007-02-06 14:06:13
现在程序源代码如下,其实这个功能还是有很多作用的,而且我又提倡开源,所以把代码公布出来,贡大家参考,当然功能的大部分工作还有待以后来完善!!!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.regex.*;
import javax.swing.event.*;
class Win1 extends JFrame implements ActionListener,Runnable
{
JButton button;
URL url, newURL;
JTextField text;
JEditorPane editPane;
byte b[]=new byte[118];
Thread thread;
Container con=null;
JPanel p;
public Win1()
{
text=new JTextField(20);
editPane=new JEditorPane();
editPane.setEditable(false);
button=new JButton("确定");
button.addActionListener(this);
thread=new Thread(this);
p=new JPanel();
p.add(new JLabel("输入网址:"));
p.add(text);
p.add(button);
con=getContentPane();
con.add(new JScrollPane(editPane),BorderLayout.CENTER);
con.add(p,BorderLayout.NORTH);
setBounds(60,60,460,380);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(!(thread.isAlive()))
thread=new Thread(this);
try{
thread.start();
}
catch(Exception ee)
{
text.setText("我正在读取"+url);
}
}
public void run()
{
try {
int m=-1;
editPane.setText(null);
url=new URL(text.getText().trim());
InputStream in=url.openStream();
File file=new File("temp.html");
ByteArrayOutputStream write=new ByteArrayOutputStream ();
while((m=in.read(b))!=-1)
{
write.write(b,0,m);
}
write.close();
in.close();
byte content[]=write.toByteArray();
String str=new String(content);
Pattern pattern;
Matcher match;
// pattern=Pattern.compile("<IMG.*>",Pattern .CASE_INSENSITIVE);
// match=pattern.matcher(str);
//str=match.replaceAll("");
pattern=Pattern.compile("新闻",Pattern .CASE_INSENSITIVE);
match=pattern.matcher(str);
str=match.replaceAll("****");
byte cc[]=str.getBytes();
ByteArrayInputStream inByte=new ByteArrayInputStream(cc);
FileOutputStream out=new FileOutputStream(file);
byte dd[]=new byte[1024];
while((m=inByte.read(dd,0,1024))!=-1)
{
out.write(dd,0,m);
}
out.close();
inByte.close();
newURL=file.toURL();
con.removeAll();
editPane=new JEditorPane();
editPane.setEditable(false);
editPane.addHyperlinkListener(new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType()==
HyperlinkEvent.EventType.ACTIVATED)
{
try{
URL linkURL=e.getURL();
editPane.setPage(linkURL);
}
catch(IOException e1)
{
editPane.setText(""+e1);
}
}
}
}
);
editPane.setPage(newURL);
con.add(p,BorderLayout.NORTH);
con.add(new JScrollPane(editPane),BorderLayout.CENTER);
con.validate();
validate();
}
catch(MalformedURLException e1)
{
text.setText(""+e1);
return;
}
catch(IOException e1)
{
text.setText(""+e1);
return;
}
}
}
public class Win
{
public static void main(String args[])
{
new Win1();
}
}