import
org.jsoup.Connection;
import
org.jsoup.HttpStatusException;
import
org.jsoup.Jsoup;
import
java.io.*;
import
java.util.ArrayList;
import
java.util.List;
/**
* [url=home.php?mod=space&uid=686208]@AuThor[/url] 三木猿
* [url=home.php?mod=space&uid=1248337]@version[/url] 1.0
* @Title:
* [url=home.php?mod=space&uid=686237]@date[/url] 2020/9/7 10:43
*/
public
class
test2 {
public
static
void
main(String[] args) {
try
{
List<Thread> threads=
new
ArrayList<>(
3
);
long
start = System.currentTimeMillis();
for
(
int
i =
0
; i <
3
; i++) {
final
int
ins=i;
Thread thread =
new
Thread(() -> {
try
{
System.out.println(
"线程"
+Thread.currentThread().getName()+
"正在运行"
);
downloadImg(
10
*ins, (ins+
1
)*
10
);
}
catch
(Exception e) {
e.printStackTrace();
}
});
thread.start();
threads.add(thread);
}
for
(Thread thread : threads) {
thread.join();
}
long
end = System.currentTimeMillis();
System.out.println(
"所有下载已完成,本次共用时"
+(end-start)/
1000
+
"s"
);
}
catch
(Exception e) {
e.printStackTrace();
}
}
public
static
void
downloadImg(
int
start,
int
end)
throws
Exception {
if
(start > end) {
System.out.println(
"输入的参数有误"
);
}
for
(
int
i = start; i < end; i++) {
String url =
"https://lns.hywly.com/a/1/"
+ i +
"/"
;
int
count =
100
;
for
(
int
j =
0
; j < count; j++) {
String src =
"d:/SanMu/image/"
+ i +
"/"
;
File file1 =
new
File(src + j +
".jpg"
);
if
(!file1.exists()) {
Connection.Response execute =
null
;
try
{
execute = Jsoup.connect(url + j +
".jpg"
).ignoreContentType(
true
).execute();
}
catch
(HttpStatusException httpStatusException) {
break
;
}
catch
(IOException e) {
e.printStackTrace();
}
assert
execute !=
null
;
File file =
new
File(src);
if
(!file.exists()) {
file.mkdir();
}
file1.createNewFile();
FileOutputStream fos =
new
FileOutputStream(file1);
BufferedOutputStream bos =
new
BufferedOutputStream(fos);
bos.write(execute.bodyAsBytes());
bos.close();
fos.close();
}
}
}
}
}