今天从网上下载资料,全下载要点开100个页面,每个页面还要点击另存,我一下子犯愁了,咋整呢?
于是草草的干了一堆代码,用多线程批量下载。
[java]
package downjianai;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
[/java]
[java]
import java.net.MalformedURLException;
import java.net.URL;
public class Down {
public static void main(String[] args) throws MalformedURLException, IOException {
//下载的资源共100个,研究发现前99个地址是有规律的,于是创建11组线程,每个线程下载9个MP3刚刚好;
//剩下的1个不规则地址单独下载
for(int i=1;i<12;i++) {
new Thread(new ThreadDown((i-1)*9+1)).start();
}
//单独下载的线程
try {
URL url = new URL("http://listen.putclub.com/resource/mingzhu/jianai/putclub.com_jane100.mp3");
InputStream is = url.openStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\tanmingxin\\Desktop\\down\\jane100.mp3"));
byte[] buf = new byte[1024*1024];
int len = 0;
while((len=bis.read(buf))!=-1) {
bos.write(buf, 0, len);
bos.flush();
}
bos.close();
bis.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ThreadDown implements Runnable {
private int index;
ThreadDown(int index) {
this.index = index;
}
public void run() {
try {
for(int i=index;i<index+9;i++) {
URL url = new URL("http://listen.putclub.com/resource/mingzhu/jianai/putclub.com_jane"+i+".mp3");
InputStream is = url.openStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\tanmingxin\\Desktop\\down\\jane"+i+".mp3"));
byte[] buf = new byte[1024*1024];
int len = 0;
while((len=bis.read(buf))!=-1) {
bos.write(buf, 0, len);
bos.flush();
}
bos.close();
bis.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
[/java]
8 条评论
I have to express my love for your generosity giving support to those individuals that must have assistance with this particular area. Your very own dedication to getting the solution all over was extremely practical and have in most cases made ladies just like me to arrive at their ambitions. Your new informative tips and hints denotes much a person like me and even more to my office colleagues. Many thanks; from everyone of us.
I would like to show thanks to you for bailing me out of this particular issue. After exploring throughout the online world and finding tips which are not helpful, I thought my entire life was gone. Being alive without the presence of strategies to the issues you have sorted out all through the write-up is a crucial case, as well as those which could have adversely damaged my entire career if I hadn't encountered your website. Your main ability and kindness in controlling all the things was very useful. I don't know what I would have done if I hadn't come upon such a subject like this. I'm able to now relish my future. Thanks a lot very much for your professional and results-oriented help. I will not think twice to recommend your blog to any person who needs to have tips on this matter.
I and my buddies happened to be viewing the good tips and hints from your web page and then immediately I had a terrible feeling I had not thanked the web blog owner for those tips. All the young boys are already totally very interested to read through all of them and have simply been loving these things. Thank you for truly being so considerate and for settling on this form of good areas millions of individuals are really wanting to learn about. My sincere regret for not saying thanks to sooner.
Vielen Dank für die gemeinsame Nutzung.
Hola y gracias por este blog es una verdadera inspiración ..
It is truly a great and helpful piece of info. I’m happy that you simply shared this useful information with us. Please keep us informed like this. Thank you for sharing.
hello I love the header of your blog, it is a personal creation?
yes,this is completely informal, because i don know how to make it, welcome to my blog.