【java学习记录】13.实现简单的浏览器功能:获取源代码、保存源代码、获取图片

仿照IE浏览器,设计一简单的网站访问程序,实现如下功能:
(1)具有类似IE的简单界面,使用者能输入URL地址,获得输入URL的HTML源代码;
(2)将获得的HTML源代码保存到指定文件中。D:\index.html
(3)从获得的HTML源代码中,选择一个图片(.jpg,.gif,.png)链接,从WWW服务器获取该文件并下载保存。D:\pic8.jpg

Browser

public class Browser extends JFrame implements ActionListener{

    JButton btnGetSource;//获取源代码
    JButton btnSaveSource;//保存源代码
    JButton btnGetImage;//获取图片
    JTextField urlText;//网址文本框
    JTextArea sourceText;//源代码文本域

    Browser(){

        //框架
        this.setTitle("浏览器");
        this.setSize(800, 500);
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//结束程序
        this.setLayout(new BorderLayout());//使用边框布局


        //面板
        JPanel northPanel=new JPanel();//北区面板
        JPanel centerPanel=new JPanel();//中区面板
        JPanel btnPanel=new JPanel();//存放按钮的面板
        northPanel.setBackground(Color.white);//设置背景颜色
        centerPanel.setBackground(Color.cyan);
        btnPanel.setBackground(Color.white);
        northPanel.setPreferredSize(new Dimension(0, 38));//设置北区面板的高度为45,宽度随窗口变化
        northPanel.setLayout(new BorderLayout());//使用边框布局
        centerPanel.setLayout(new BorderLayout());//使用边框布局
        this.add(northPanel,BorderLayout.NORTH);//添加到框架的北区
        this.add(centerPanel,BorderLayout.CENTER);//添加到框架的中区
        northPanel.add(btnPanel,BorderLayout.EAST);//将btnPanel面板添加到northPanel面板的东区

        //URL标签
        JLabel urlLabel=new JLabel("URL:");
        urlLabel.setBorder(new EmptyBorder(0, 5, 0, 0));
        urlLabel.setPreferredSize(new Dimension(40,0));
        northPanel.add(urlLabel,BorderLayout.WEST);

        //网址文本框
        urlText=new JTextField("http://www.swust.edu.cn");
        urlText.setBorder(new CompoundBorder(new EmptyBorder(5,0,5,0), new LineBorder(Color.gray)));//设置文本框的位置和显示文本框的边框
        northPanel.add(urlText,BorderLayout.CENTER);

        //按钮
        btnGetSource=new JButton("获取源代码");  
        btnSaveSource=new JButton("保存源代码");
        btnGetImage=new JButton("获取图片");
        btnPanel.add(btnGetSource);//将按钮添加到btnPanel面板上
        btnPanel.add(btnSaveSource);
        btnPanel.add(btnGetImage);
        //添加监听器
        btnGetSource.addActionListener(this);
        btnSaveSource.addActionListener(this);
        btnGetImage.addActionListener(this);

        //源代码文本域
        sourceText=new JTextArea();
        JScrollPane scrollPane=new JScrollPane(sourceText);//给文本域添加滚动条
        scrollPane.setBorder(new CompoundBorder(new EmptyBorder(0,5,15,5), new LineBorder(Color.gray)));//设置文本框的位置和显示文本域的边框
        centerPanel.add(scrollPane);

        //显示框架
        this.setVisible(true);
    }

    //点击事件
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==btnGetSource){
            getSource();//获取源代码
        }else if(e.getSource()==btnSaveSource){
            saveSource();//保存源代码
        }else{
            getImage();//获取图片
        }
    }

    //获取源代码
    public void getSource(){
//      System.out.println("111");
        sourceText.setText("");//清空源代码文本域内容
        try {
            URL url=new URL(urlText.getText());//获取网址
            InputStream is=url.openStream();//打开网址
            InputStreamReader isr=new InputStreamReader(is,"utf-8");//读取网址内容,UTF-8:编码方式
            BufferedReader br=new BufferedReader(isr);//设置缓冲
            String read;//存储读取的内容
            while((read=br.readLine())!=null){//一行一行的读取内容
                sourceText.append(read+"\n");//添加到源代码文本域中
            }
            //关闭流
            br.close();
            isr.close();
            is.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    //保存源代码
    public void saveSource(){
//      System.out.println("222");
        File source=new File("D:\\index.html");//创建txt文本
        try {
            FileWriter fw=new FileWriter(source);//字符输出流
            String read=sourceText.getText();//获取源代码文本域内容
            fw.write(read);
            //弹出提示框
            JOptionPane.showMessageDialog(this, "源代码保存成功!");
            fw.close();//关闭流
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    //获取图片
    public void getImage(){
//      System.out.println("333");
        try {
            URL url=new URL("http://www.swust.edu.cn/page/main851/images/flash1.jpg");//图片地址
            InputStream is=url.openStream();//打开网址
            File image=new File("D:\\pic8.jpg");//创建文件
            FileOutputStream fos=new FileOutputStream(image);//输出流
            byte[] b=new byte[2048];//需为2的倍数
            int n=is.read(b);//读取字节的个数,每次读2048个字节
            while(n!=-1){//-1表示读完
//              fos.write(b1);//写入所有字节流
                fos.write(b,0,n);//第0个字节开始,写入n个字节
                n=is.read(b);
            }
            //弹出提示框
            JOptionPane.showMessageDialog(this, "图片获取成功!");
            //关闭流
            is.close();
            fos.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Browser();
    }

}

获取源代码

保存源代码

获取图片

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值