首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

封存用户网址,并对网址进行校验

2012-11-23 
保存用户网址,并对网址进行校验package com.alfusen.clientimport java.awt.BorderLayoutimport java.aw

保存用户网址,并对网址进行校验
package com.alfusen.client;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.alfusen.utl.GeneralTools;

/**
* 网址输入框
* @author  Administrator
* @version  [版本号, 2012-9-10]
*/
public class ClientAddrDialog extends JDialog implements ActionListener
{
   
    /**
     * 序列号
     */
    private static final long serialVersionUID = -7024012529868588166L;
   
    Client client = null;
   
    public ClientAddrDialog(Client jf, String name)
    {
        this.setTitle(name);
        this.client = jf;
        try
        {
            init();
        }
        catch (ClassNotFoundException e)
        {
        }
       
    }
   
    public JTextArea jta = null;
   
    public JButton jbt = new JButton("确定");
   
    public JButton clear = new JButton("取消");
   
    public JLabel jlb = new JLabel("请输入网址:");
   
    public JLabel lable = new JLabel("最多只能输入三个网址");
   
    public JPanel jpanel = new JPanel();
   
    public JPanel jpane_north = new JPanel();
   
    public void init() throws ClassNotFoundException
    {
        GeneralTools.setIcon(this);
       
        this.setModal(true);
        this.setResizable(false);
       
        //屏幕置中
        //setLocationRelativeTo(null);
        this.setLocation(400, 320);
       
        if (!client.getAppList().isEmpty())
        {
            List<String> list = client.getAppList();
            jta = new JTextArea();
            for (int i = 0; i < list.size(); i++)
            {
                if (i == 0)
                {
                    jta.setText(list.get(i));
                }
                else
                {
                    jta.setText(jta.getText() + "\n" + list.get(i));
                }
            }
        }
        else
        {
            jta = new JTextArea("http://");
        }
        jta.setLineWrap(true);
       
        //添加确定和取消按钮
        jpanel.add(jbt);
        jpanel.add(clear);
       
        Container contentPane = getContentPane();
        contentPane.add(jlb, BorderLayout.NORTH);
        contentPane.add(new JScrollPane(jta), BorderLayout.CENTER);
        contentPane.add(jpanel, BorderLayout.SOUTH);
       
        this.setSize(415, 200);
       
        //添加事务
        jbt.addActionListener(this);
        clear.addActionListener(this);
    }
   
    public void close()
    {
        this.dispose();
    }
   
    /**
     * 事务监听
     * @param e 监听器
     */
    public void actionPerformed(ActionEvent e)
    {
        Object obj = e.getSource();
        if (obj == jbt)
        {
            String addrs = jta.getText();
            if (null == addrs || "".equals(addrs.trim()))
            {
                JOptionPane.showMessageDialog(jta,
                        "请输入网址",
                        "请输入网址",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
           
            //网址数组
            String[] addr = addrs.split("\n");
           
            String[] addrDest = null;
           
            if (addr.length > 0 && "".equals(addr[addr.length - 1].trim()))
            {
                addrDest = new String[addr.length - 1];
                System.arraycopy(addr, 0, addrDest, 0, addr.length - 1);
            }
            else
            {
                addrDest = addr;
            }
           
            //判断长度
            if (addrDest.length > 3)
            {
                JOptionPane.showMessageDialog(jta,
                        "最多只能输入三个网址,谢谢!",
                        "请输入网址",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
           
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < addrDest.length; i++)
            {
                if ("".equals(addrDest[i].trim())
                        || addrDest[i].trim().matches("\r\n")
                        || addrDest[i].trim().matches("\n"))
                {
                    JOptionPane.showMessageDialog(jta,
                            "网址不合法,中间不能空行",
                            "请输入网址",
                            JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (!addrDest[i].trim().matches("http://[^/]+/.*"))
                {
                    JOptionPane.showMessageDialog(jta,
                            "网址" + addrDest[i] + "不合法,必须http://开头",
                            "请输入网址",
                            JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (addrDest[i].length() > 255)
                {
                    JOptionPane.showMessageDialog(jta,
                            "网址" + addrDest[i] + "不合法,\n长度必须小于255",
                            "请输入网址",
                            JOptionPane.WARNING_MESSAGE);
                    return;
                }
                list.add(addrDest[i]);
            }
           
            //检测网址是否能访问
            if (!checkAddr(list))
            {
                JOptionPane.showMessageDialog(jta,
                        "网址不合法,\n请确认地址输入正确",
                        "请输入网址",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
           
            //清空原来的值原来的值
            client.getAppList().clear();
           
            //重新添加网址
            client.getAppList().addAll(list);
           
            if (list.size() < 3)
            {
                for (int i = 3; i > list.size(); i--)
                {
                    GeneralTools.saveCfg(client.getPs(), "addr" + i, "");
                }
            }
           
            //写入mm.cfg文件中
            for (int i = 0; i < list.size(); i++)
            {
                GeneralTools.saveCfg(client.getPs(),
                        "addr" + (i + 1),
                        list.get(i));
            }
           
            //关闭窗口
            close();
        }
        else if (clear == obj)
        {
            close();
        }
    }
   
    /**
     * 检查地址是否可以用
     * @param list
     * @return [参数说明]
     *
     * @return boolean [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private boolean checkAddr(List<String> list)
    {
        for (String string : list)
        {
            URL url;
            URLConnection con;
            try
            {
                url = new URL(string);
                con = url.openConnection();
               
                //con.connect();
                InputStream input = con.getInputStream();
               
                if (input.available() == 0)
                {
                    return false;
                }
            }
            catch (MalformedURLException e)
            {
                return false;
            }
            catch (IOException e)
            {
                return false;
            }
        }
        return true;
    }
   
}

热点排行