问题描述
如题我需要的最好是具体的代码谢谢各位大侠
解决方案
解决方案二:
是的,如果用Swing的话,就是在监听器的actionPerformed方法中做。
解决方案三:
importjava.awt.*;importjava.awt.event.*;importjava.net.*;importjava.io.*;publicclassDownLoadimplementsActionListener{privateFramef;privateTextFieldtf;privatePanelp1,p2,p3;privateTextAreata;privateLabell1,l2;privateButtonb;publicstaticvoidmain(Stringargs[]){newDownLoad().init();}publicvoidinit(){f=newFrame("蝴蝶下载程序");f.setSize(600,400);f.setLocation(100,100);l1=newLabel("请输入URL:");l2=newLabel("蝴蝶下载支持的类型为:html,jpg,txt,psd,wav,doc,ppt,bmp,exe,mp3,rm,jsp,zip,gif,class");tf=newTextField(30);p1=newPanel();p2=newPanel();p1.add(l1);p1.add(tf);p2.add(l2);p3=newPanel();p3.setLayout(newGridLayout(2,1));p3.add(p1);p3.add(p2);f.add("North",p3);ta=newTextArea();f.add(ta);b=newButton("下载");f.add("South",b);b.addActionListener(this);f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){f.setVisible(false);f.dispose();System.exit(0);}});f.setVisible(true);}publicvoidactionPerformed(ActionEvente){Stringstr=tf.getText();try{URLurl=newURL(str);URLConnectionurlcon=url.openConnection();Stringline=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);Stringtype=urlcon.getContentType();if(type.equals("text/html"))type="html";elseif(type.equals("text/plaign"))type="txt";elseif(type.equals("image/x-photoshop"))type="psd";elseif(type.equals("audio/x-wav"))type="wav";elseif(type.equals("application/msword"))type="doc";elseif(type.equals("application/powerpoint"))type="ppt";elseif(type.equals("image/bmp"))type="bmp";elseif(type.equals("exe"))type="exe";elseif((type.equals("audio/x-mpeg"))|(type.equals("audio/mpeg")))type="mp3";elseif(type.equals("application/vnd.rn-realmedia"))type="rm";elseif(type.equals("text/html;charset=ISO-8859-1"))type="jsp";elseif(type.equals("application/zip"))type="zip";elseif(type.equals("image/gif"))type="gif";elseif(type.equals("application/java"))type="class";elseif(type.equals("image/jpeg"))type="jpg";InputStreamis=urlcon.getInputStream();//InputStreamReaderipr=newInputStreamReader(is);//BufferedReaderbr=newBufferedReader(ipr);FileOutputStreamfos=newFileOutputStream("d:/Download/Download."+type);//StringstrLine;//while((strLine=br.readLine())!=null)//{//fos.write(strLine.getBytes());//fos.write(line.getBytes());//}//br.close();intdata;while((data=is.read())!=-1){fos.write(data);}fos.close();is.close();ta.append("下载完毕!");ta.append(line);}catch(Exceptionex){ta.append("下载错误");ta.append(System.getProperty("line.seperator"));}}}
在学校的时候写的一个简单程序,楼主可以看看!