URL

来源:互联网 发布:奇葩的淘宝评价 编辑:程序博客网 时间:2024/04/27 07:15
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();}}