如题   我需要的最好是具体的代码   谢谢各位大侠

解决方案 »

  1.   

    是的,如果用Swing的话,就是在监听器的actionPerformed方法中做。
      

  2.   


    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    public class DownLoad implements ActionListener
    {
        private Frame f;
        private TextField tf;
        private Panel p1,p2,p3;
        private TextArea ta;
        private Label l1,l2;
        private Button b;
        public static void main(String args [])
        {
            new DownLoad().init();
        }
        public void init()
        {
            f=new Frame("蝴蝶下载程序");
            f.setSize(600,400);
            f.setLocation(100,100);
            l1=new Label("请输入URL:");
            l2=new Label("蝴蝶下载支持的类型为:html,jpg,txt,psd,wav,doc,ppt,bmp,exe,mp3,rm,jsp,zip,gif,class");
            tf=new TextField(30);
            p1=new Panel();
            p2=new Panel();
            p1.add(l1);
            p1.add(tf);
            p2.add(l2);
            p3=new Panel();
            p3.setLayout(new GridLayout(2,1));
            p3.add(p1);
            p3.add(p2);
            f.add("North",p3);
            ta=new TextArea();
            f.add(ta);
            b=new Button("下载");
            f.add("South",b);
            b.addActionListener(this);
            f.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    f.setVisible(false);
                    f.dispose();
                    System.exit(0);
                }
            });
            f.setVisible(true);    
        }
        public void actionPerformed(ActionEvent e)
        {
            String str=tf.getText();
            try
            {
                URL url=new URL(str);
                URLConnection urlcon=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:"+urlcon.getContentType());
                ta.append(line);
                ta.append("ContentLength:"+urlcon.getContentLength());  
                ta.append(line);
                String type=urlcon.getContentType();
                if(type.equals("text/html"))
                    type="html";
                else if(type.equals("text/plaign"))
                    type="txt";
                else if(type.equals("image/x-photoshop"))
                    type="psd";
                else if(type.equals("audio/x-wav"))
                    type="wav";
                else if(type.equals("application/msword"))
                    type="doc";
                else if(type.equals("application/powerpoint"))
                    type="ppt";
                else if(type.equals("image/bmp"))
                    type="bmp";
                else if(type.equals("exe"))
                    type="exe";
                else if((type.equals("audio/x-mpeg")) | (type.equals("audio/mpeg")))
                    type="mp3";
                else if(type.equals("application/vnd.rn-realmedia"))
                    type="rm";
                else if(type.equals("text/html;charset=ISO-8859-1"))
                    type="jsp";
                else if(type.equals("application/zip"))
                    type="zip";
                else if(type.equals("image/gif"))
                    type="gif";
                else if(type.equals("application/java"))
                    type="class";    
                else if(type.equals("image/jpeg"))
                    type="jpg";
                InputStream is=urlcon.getInputStream();
                //InputStreamReader ipr=new InputStreamReader(is);
               // BufferedReader br=new BufferedReader(ipr);
                FileOutputStream fos=new FileOutputStream("d:/Download/Download."+type);
                //String strLine;
               //while((strLine=br.readLine())!=null)
                //{
                   // fos.write(strLine.getBytes());
                   // fos.write(line.getBytes());
               // }
                //br.close();
                int data;
                while((data=is.read())!=-1)
                {
                    fos.write(data);
                }
                fos.close();
                is.close();
                ta.append("下载完毕!");
                ta.append(line);
            }
            catch(Exception ex)
            {
                ta.append("下载错误");
                ta.append(System.getProperty("line.seperator"));
            }
                    
        }}在学校的时候写的一个简单程序,楼主可以看看!