java浏览器开发

package wap;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.*;
import java.lang.reflect.Array;
import java.net.*;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

//将网页保存在本地(通过url,和保存的名字)



class jin extends JFrame implements HyperlinkListener  {
	 JTextField s; JButton b; JEditorPane j;

	jin(){
		
	setBounds(100,10, 900, 700);
setTitle("浏览器");
	j=new  JEditorPane();
	s =new  JTextField();
	b= new JButton("→");
	 
	 j.setEditable(false);     //请把editorPane设置为只读,不然显示就不整齐
	 JScrollPane scrollPane = new JScrollPane(j); 
		s.setText("http://wapjin.com");
      	
        try {
			j.setPage(s.getText());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		setLayout(null);
		s.setBounds(200, 10, 300, 40);
		scrollPane.setBounds(0, 60, 900, 630);
		b.setBounds(500, 10, 60, 40);
		add(b);
	add(s);
	 add(scrollPane);
	 j.addHyperlinkListener(this); 
		setVisible(true);
		b.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				 Save_Html(s.getText().toString());
				 try {
		                String encoding="GBK";
		                File file=new File("D:/html/1.html");
		                if(file.isFile() && file.exists()){ //判断文件是否存在
		                    InputStreamReader read = new InputStreamReader(
		                    new FileInputStream(file),encoding);//考虑到编码格式
		                    BufferedReader bufferedReader = new BufferedReader(read);
		                   String lineTxt = null;  StringBuilder sb = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中
		                 while((lineTxt = bufferedReader.readLine()) != null){
		                	 sb.append(lineTxt+ "
");
		                    }
		                    	//System.out.print(sb);
		                    	
		               j.setPage(s.getText());
		                    read.close();
		        }else{
		            System.out.println("找不到指定的文件");
		        }
		        } catch (Exception e) {
		            System.out.println("读取文件内容出错");
		            e.printStackTrace();
		        }
				
				
			}

		});
		
	}
	
	
	//将抓取的网页变成html文件,保存在本地
    public static void Save_Html(String url) {
        try {
            File dest = new File("D:/html/" + "1.html");
            File fileParent = dest.getParentFile();  
            if(!fileParent.exists()){  
                fileParent.mkdirs();  
            }  
            dest.createNewFile();
            //接收字节输入流
            InputStream is;
            //字节输出流
            FileOutputStream fos = new FileOutputStream(dest);
    
            URL temp = new URL(url);
            is = temp.openStream();
            
            //为字节输入流加缓冲
            BufferedInputStream bis = new BufferedInputStream(is);
            //为字节输出流加缓冲
            BufferedOutputStream bos = new BufferedOutputStream(fos);
    
            int length;
    
            byte[] bytes = new byte[1024*20];
            while((length = bis.read(bytes, 0, bytes.length)) != -1){
                fos.write(bytes, 0, length);
            }

            bos.close();
            fos.close();
            bis.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


public void hyperlinkUpdate(HyperlinkEvent e)  
{  
   if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)  
   {  
      JEditorPane pane = (JEditorPane) e.getSource();  
      if (e instanceof HTMLFrameHyperlinkEvent)  
      {  
         HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;  
         HTMLDocument doc = (HTMLDocument) pane.getDocument();  
         doc.processHTMLFrameHyperlinkEvent(evt);  
      }  
      else  
      {  
         try  
         {  
            pane.setPage(e.getURL()); 
            System.out.print(e.getURL());
            s.setText(e.getURL().toString());
         }  
         catch (Throwable t)  
         {  
            t.printStackTrace();  
         }  
      }  
   }  
}



}
public class wapjin {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
new jin();
	}

}

评论

刷新

友情链接