一个简单的下载程序

Download.java

<span style="font-size:14px;">package Download;

import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class Download
{
  public static void main(String[] args)
  {
    JFrame jf=new JFrame("我的下载程序");
    jf.setSize(600,400);
    jf.setLocation(100,100);
    JPanel p=new JPanel();
    JLabel l=new JLabel("Please input URL:");
    final JTextField tf=new JTextField(30);
    p.add(l);
    p.add(tf);
    jf.getContentPane().add(p,"North");
    final JTextArea ta=new JTextArea();
    jf.getContentPane().add(ta,"Center");
    JButton btn=new JButton("Download");
    jf.getContentPane().add(btn,"South");
    
    
    btn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            String str=tf.getText();
            try {
              URL url=new URL(str);
              URLConnection urlConn=url.openConnection();
              String line=System.getProperty("line.separator");
              ta.append("Host: "+url.getHost());
              ta.append(line);
              ta.append("Port: "+url.getDefaultPort());
              ta.append(line);
              ta.append("ContentType: "+urlConn.getContentType());
              ta.append(line);
              ta.append("ContentLength: "+urlConn.getContentLength());
              InputStream is=urlConn.getInputStream();
              //InputStreamReader isr=new InputStreamReader(is);
              //BufferedReader br=new BufferedReader(isr);
              FileOutputStream fos=new FileOutputStream("1.html");
              //String strLine;
              //while((strLine=br.readLine())!=null)
              int data;
              while((data=is.read())!=-1)
              {
//                fos.write(strLine.getBytes());
  //              fos.write(line.getBytes());
                fos.write(data);
              }
              //br.close();
              is.close();
              fos.close();
            }
            catch (Exception ex) {
              ex.printStackTrace();
            }

          }
        });
    jf.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
           System.exit(0);
         }
       });
    jf.show();
  }
}



</span>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值