package Run; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; class Main { public static void main(String[] args) { DownLoad d1 = new DownLoad("http://s1.music.126.net/download/pc/cloudmusicsetup_2_1_2[178132].exe"); DownLoad d2 = new DownLoad("http://psoft.xpgod.com:801/small/emacs-_xpgod.rar"); Thread t1 = new Thread(d1); Thread t2 = new Thread(d2); t1.start(); t2.start(); } } class DownLoad implements Runnable { private String stringUrl; public DownLoad(String url) { stringUrl = url; } private String getFileName(String url) { char[] ch = url.toCharArray(); int i = ch.length - 1; while ( ch[i] != '/') i--; char[] name = new char[ch.length - i]; for (int j = 0; i != ch.length; ++j,++i) name[j] = ch[i]; return new String(name); } public void run() { try { URL url = new URL(stringUrl); URLConnection con = url.openConnection(); InputStream in = con.getInputStream(); FileOutputStream fs = new FileOutputStream("d:/" + getFileName(stringUrl) ); byte[] buf = new byte[1024]; int bytes = -1; while ( (bytes = in.read(buf)) != -1) { fs.write(buf, 0, bytes); } fs.close(); } catch (MalformedURLException e) { System.out.println("URL非法!"); } catch (IOException e) { System.out.println("无法打开链接!"); } } }
浙公网安备 33010602011771号