首页 > 代码库 > 离线网页制作器(beta1.0)

离线网页制作器(beta1.0)

  1 package hhuarongdao;  2 /*  3  *使用方法: 先选择保存路径,然后输入相应的网址,  4  *然后会得到那个网页的离线版的 内容   5  * */  6 import java.awt.BorderLayout;  7 import java.awt.Container;  8 import java.awt.FlowLayout;  9 import java.awt.Font; 10 import java.awt.event.ActionEvent; 11 import java.awt.event.ActionListener; 12 import java.io.File; 13 import java.io.FileNotFoundException; 14 import java.io.FileOutputStream; 15 import java.io.IOException; 16 import java.io.InputStream; 17 import java.io.OutputStream; 18 import java.net.MalformedURLException; 19 import java.net.URL; 20 import java.util.Scanner; 21 import javax.swing.ImageIcon; 22 import javax.swing.JButton; 23 import javax.swing.JFileChooser; 24 import javax.swing.JFrame; 25 import javax.swing.JLabel; 26 import javax.swing.JOptionPane; 27 import javax.swing.JPanel; 28 import javax.swing.JTextField; 29  30 public class stu { 31     public static void main(String args []){ 32     setBjing mybe= new setBjing(); 33     } 34 } 35  36 class mywndow extends JFrame 37  { 38    mywndow(String title){ 39         setTitle(title); 40         setBounds(200,100 ,400,300); 41         this.setResizable(false); 42       } 43  } 44 //设置一个背景 45 class setBjing implements ActionListener 46 { 47      URL url;     48      look it=new look(); 49      Thread readurl; 50      Container com; 51      JButton  button,button1 ; 52      JTextField text,mytext ; 53      setBjing(){ 54       mywndow hehe =new mywndow("离线网页制作器1.bate"); 55       com=hehe.getContentPane(); 56       com.setLayout(new FlowLayout()); 57       mytext =new JTextField(20); 58       mytext.setEditable(false);   //设置成为不能 59       mytext.setFont(new Font("Arial",Font.BOLD,22)); 60       button1 = new JButton("选择路径"); 61       button1.addActionListener(this); 62       button = new JButton("Submitt"); 63       button.addActionListener(this); 64       text = new JTextField(20); 65       text.addActionListener(this); 66       JLabel label = new JLabel("输入网址,如:http://www.baidu.com"); 67       com.add(label); 68       com.add(text); 69       com.add(button); 70       com.add(button1); 71       com.add(mytext); 72       hehe.setVisible(true); 73       hehe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 74       //text.setText("输入网址,例如:http://www.baidu.com"); 75     } 76       public  void work(String ss){ 77               try { 78                 url = new URL(ss); 79                 it.setURL(url); 80                 readurl = new Thread(it); 81                 readurl.start(); 82              } 83             catch (Exception exp) 84             { 85                 // TODO Auto-generated catch block 86              JOptionPane.showMessageDialog(text,exp,"离线网页制作器1.bate",JOptionPane.ERROR_MESSAGE); 87             } 88              } 89      @Override 90     public void actionPerformed(ActionEvent e) { 91         // TODO Auto-generated method stub 92         if(e.getSource()==button){ 93             String ss=text.getText().toString().trim();   94             work(ss); 95             text.setText(null); 96         } 97         else if(e.getSource()==button1){ 98             JFileChooser chooser = new JFileChooser(); 99             chooser.setCurrentDirectory(new File("D:/")); //默认路径为D盘100             chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//文件选择模式,只读101             int index = chooser.showDialog(null, "确定");102             if(index==chooser.APPROVE_OPTION){103              it.setPath(chooser.getSelectedFile().getAbsolutePath());104              mytext.setText(chooser.getSelectedFile().getAbsolutePath().toString());105              //validate();106             }107         }108     }109 }110 class look implements Runnable111 {112     URL url;113     public File fwrite = new File("url.html");114    void setPath(String road)115    {116      fwrite = new File(road+".html");117    }118     public void setURL(URL url){119         this.url=url;    120     }121     @Override122     public void run() {                          123         // TODO  Auto-generated method stub124       try{125           OutputStream  out = new FileOutputStream(fwrite,true);126           InputStream in=url.openStream();127           byte [] a=new byte [1024];128           int n=-1;129          while( (n=in.read(a))!=-1 ){130              out.write(a, 0,n);131          }132         out.close();133        JOptionPane.showMessageDialog(null,"保存成功!","离线网页制作器1.bate",JOptionPane.ERROR_MESSAGE);134       }135      catch(IOException e){136         JOptionPane.showMessageDialog(null,"文件保存失败!","离线网页制作器1.bate",JOptionPane.ERROR_MESSAGE);137        };138     }139 }