📄 urlfile.java
字号:
import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;public class UrlFile extends Frame implements ActionListener{ private URL url; Label lblHost,lblIP,lblPort,lblProtocol,lblFile; TextField FileName; Button getButton; public UrlFile() { super("利用URL得到网络文件"); setSize(400,400); setBackground(Color.lightGray); setGUI(); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); pack(); show(); } private void setGUI(){ Panel pNorth=new Panel(); pNorth.add(new Label("URL文件:")); pNorth.add(FileName=new TextField()); FileName.setColumns(25); pNorth.add(getButton=new Button("提取文件信息")); getButton.addActionListener(this); this.add("North",pNorth); Panel pCenter=new Panel(); pCenter.setLayout(new GridLayout(5,2)) ; pCenter.add(new Label("主机名: ",Label.RIGHT )); pCenter.add(lblHost=new Label("",Label.LEFT )); pCenter.add(new Label("主机IP地址: ",Label.RIGHT )); pCenter.add(lblIP=new Label("",Label.LEFT)); pCenter.add(new Label("端口: ",Label.RIGHT )); pCenter.add(lblPort=new Label("",Label.LEFT)); pCenter.add(new Label("协议名称: ",Label.RIGHT )); pCenter.add(lblProtocol=new Label("",Label.LEFT)); pCenter.add(new Label("文件名: ",Label.RIGHT )); pCenter.add(lblFile=new Label("",Label.LEFT)); this.add("Center",pCenter); } public void actionPerformed(ActionEvent e){ Object obj = e.getSource(); if(obj==getButton) { if(url!=null) url=null; try{ url=new URL(FileName.getText() ); getInfo(); saveFile(); }catch(Exception ex){ System.out.println(ex); } } } private void getInfo(){ try{ lblHost.setText(url.getHost()); lblProtocol.setText(url.getProtocol()); InetAddress host=InetAddress.getByName(url.getHost()); lblIP.setText(host.getHostAddress()); Integer port=new Integer(url.getPort()); lblPort.setText(port.toString()); lblFile.setText(url.getPath() ); }catch(Exception e){ System.out.println(e); } } private void saveFile(){ try{ DataInputStream inStream=new DataInputStream(url.openStream() ); File file=new File("."+url.getFile() ); mkdir(file.getParent()); DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(file)); int count=0; try{ while(true) { outputStream.writeByte((int)inStream.readByte()) ; } }catch(EOFException eofEx){ inStream.close(); outputStream.flush(); outputStream.close(); System.out.println(eofEx); } }catch(Exception e){ System.out.println(e); } } private void mkdir(String strfileName) { try{ File newFile=new File(strfileName); File parentFile=new File(newFile.getParent()); if( !parentFile.exists() ) mkdir(newFile.getParent() ); newFile.mkdir(); }catch(Exception e){ } } static public void main(String argv[]){ new UrlFile(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -