URL

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.net.*;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class HtmlJFrame extends JFrame implements ActionListener,javax.swing.event.ChangeListener 
{
	private URL urls[];//URL对象数组
	private JComboBox combobox_url;//组合框,输入或者选择URL地址
	private JTextField text_attribute;//文本行,显示文件属性
	private JTabbedPane tab;
	
	public HtmlJFrame()
	{
		//基本框架
		super("查看源文档");
		this.setBounds(300,240,640,480);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		//this.setVisible(true);
		
		//组合框(在顶部)
		String[] urls={"http://java.sun.com",
					   "http://www.baidu.com/",
					   "file://localhost/D:/Program Files/Java/docs/api/index.html"};
		combobox_url=new JComboBox(urls);//组合框显示URL地址
		combobox_url.setEditable(true);
		this.getContentPane().add(combobox_url,"North");
		combobox_url.addActionListener(this);//对组合框添加事件监听。
		
		
		//选项卡窗格
		tab=new JTabbedPane();
		tab.addChangeListener(this);//对选择事件监听
		this.getContentPane().add(tab);
		
		
		//文本区域(在底部)
		text_attribute=new JTextField();
		this.getContentPane().add(text_attribute,"South");
		this.setVisible(true);
		this.urls=new URL[20];
		actionPerformed(null);//调用事件处理方法,初始化
	}
	
	public void actionPerformed(ActionEvent e)//组合框选择或者回车键敲击的时候触发执行
	{
		String urlname=(String)combobox_url.getSelectedItem();//获取组合框中项字符串
		int i=tab.getTabCount();//获取tab当前页数,即下一页的序号,从0开始计数
		try
		{
			this.urls[i]=new URL(urlname);//创建一个URL对象
			InputStreamReader in=new InputStreamReader(this.urls[i].openStream());//有字节输入流创建字符输入流,若urls[i]指定文件不存在,抛出异常,不添加tab页
			String filename=(String)this.urls[i].toString();//URL路径名
			for(int j=i-1;j>=0;j--)
			{
				if(tab.getTitleAt(j).equals(filename))
				{
					tab.setSelectedIndex(j);
					return;
				}
			}
			JEditorPane preview=new JEditorPane(this.urls[i]);//创建url编辑窗格
			
			//文本区,显示文件内容
			JTextArea text_content=new JTextArea();
			
			//分割框
			JSplitPane splitter_v=new JSplitPane(JSplitPane.VERTICAL_SPLIT);//垂直分割窗格
			splitter_v.setDividerLocation(200);//设置水平分割条的位置
			splitter_v.add(new JScrollPane(preview));
			splitter_v.add(new JScrollPane(text_content));
			tab.addTab(filename, splitter_v);//加入到选项卡窗格当中
			tab.setSelectedIndex(tab.getTabCount()-1);//指定新建页为选中状态
			
			BufferedReader bin=new BufferedReader(in);//通过字符缓冲输入流读取文件内容
			String aline=bin.readLine();
			while(aline!=null)
			{
				text_content.append(aline+"\r\n");
				aline=bin.readLine();
			}
			bin.close();
			in.close();
		}
		catch(MalformedURLException murle)
		{
			JOptionPane.showMessageDialog(this, "字符串指定未知协议错误");
		}
		catch(FileNotFoundException fe)
		{
			JOptionPane.showMessageDialog(this, "\""+urls[i].getFile()+"\"文件不存在");
		}
		catch(IOException ioex)
		{
			JOptionPane.showMessageDialog(this, "io错,读取"+urls[i].getFile()+"文件不成功");
		}
	}

	public void stateChanged(ChangeEvent e)//当单击选项卡窗格的页标题时触发执行//通过文件对象获得文件属性
	{
		String str="文件:";
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm");
		int i=tab.getSelectedIndex();
		
		
		if(urls[i].getProtocol().equals("file"))//当前ULR对象使用file协议
		{
			File afile=new File(urls[i].getFile());//获取文件对象
			str+=afile.getAbsolutePath()+"\t";//获取文件的绝对路径
			str+=afile.length()+"B\t";//获取文件长度
			str+=sdf.format(new Date(afile.lastModified()));//获取一个文件修改时间
			try
			{
				InetAddress local=InetAddress.getLocalHost();
				str+="本机名:"+local.getHostName()+",本机IP地址:"+local.getHostAddress();
			}
			catch(UnknownHostException ioe)
			{
				JOptionPane.showMessageDialog(this, "找不到指定主机的IP地址");
			}
		}
		
		
		if(urls[i].getProtocol().equals("http"))//当前使用http协议时
		{
			try
			{
				URLConnection urlconn=urls[i].openConnection();//获取连接
				str+=urls[i].toString()+"\t";//获得URL路径名
				str+=urlconn.getContentLength()+"B\t";//获取文件长度以B为单位
				str+=urlconn.getContentType()+"\t";//获取文件类型
				str+=sdf.format(new Date(urlconn.getLastModified()));
				str+="\t主机名:"+urls[i].getHost();
				str+="\tIP地址:"+InetAddress.getAllByName(urls[i].getHost());//____________________________________________________
			}
			catch(UnknownHostException ioe)
			{
				JOptionPane.showMessageDialog(this, "找不到指定主机的IP地址");
			}
			catch(IOException ioe)
			{
				JOptionPane.showMessageDialog(this, "IO错,读取"+urls[i].getFile()+"文件不成功");
			}
		}
		text_attribute.setText(str);
	}
	
	
	public static void main(String args[])
	{
		new HtmlJFrame();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值