public
class
Test {
static
String filePath =
"E:\\下载\\PMP"
;
static
String userid =
"xxxx"
;
static
String url1 =
"https://app.xxxx.com/Lesson/myLessons"
;//
1
、获取班级地址
static
String url2 =
"https://app.xxxx.com/User/myLesson"
;//
2
、获取课程地址
static
String url3 =
"https://app.xxxx.com/Lesson/detail"
;//
3
、获取课程列表
static
String url4 =
"https://app.xxxx.com/Lesson/play"
;//
3
、获取课程文件
static
Map<String, String> headMap =
new
HashMap<String, String>();
public
static
void
main(String[] args)
throws
Exception {
List<String[]> tempmap =
new
ArrayList();
filePath +=
new
Random(
10000
).nextLong() +
"\\"
;
headMap.put(
"secket"
,
"xxxxx"
);
headMap.put(
"apid"
,
"yun.aura.cn"
);
headMap.put(
"Content-Type"
,
"application/json;charset=UTF-8"
);
System.out.println(
"获取班级列表"
);
String result = SdkClient.post(url1 +
"?p=1&uid="
+ userid,
""
, headMap);
System.out.println(result);
if
(result !=
null
&& !
""
.equals(result) && result.contains(
"userInfo"
)) {
JSONObject myLessonsresult = JSON.parseObject(result);
JSONArray data = myLessonsresult.getJSONArray(
"data"
);
for
(
int
i =
0
; i < data.size(); i++) {
JSONObject o = data.getJSONObject(i);
String LessonID = o.getString(
"lesson_id"
);
String LessonName = o.getString(
"name"
);
String current_filePath1 = filePath + LessonName +
"\\"
;
System.out.println(
"创建班级目录:"
+ current_filePath1);
new
File(current_filePath1).mkdirs();
System.out.println(
"获取班级课程,id="
+ LessonID);
result = SdkClient.post(url2 +
"?lid="
+ LessonID +
"&uid="
+ userid,
""
, headMap);
System.out.println(result);
if
(result !=
null
&& !
""
.equals(result) && result.contains(
"success"
)) {
JSONObject myLessonsresult2 = JSON.parseObject(result);
JSONArray data2 = myLessonsresult2.getJSONObject(
"data"
).getJSONArray(
"data"
);
for
(
int
i2 =
0
; i2 < data2.size(); i2++) {
JSONObject o2 = data2.getJSONObject(i2);
String LessonID2 = o2.getString(
"lid"
);
String LessonName2 = o2.getString(
"name"
);
String current_filePath2 = current_filePath1 + (i2 <
9
? (
"0"
+ (i2 +
1
)) : (i2 +
1
)) +
" "
+ LessonName2 +
"\\"
;
System.out.println(
"创建课程目录:"
+ current_filePath2);
new
File(current_filePath2).mkdirs();
System.out.println(
"获取课程文件列表,lid="
+ LessonID2);
result = SdkClient.post(
url3 +
"?pkid="
+ LessonID +
"&id="
+ LessonID2 +
"&teach_type=2"
+
"&uid="
+ userid,
""
, headMap);
System.out.println(result);
if
(result !=
null
&& !
""
.equals(result) && result.contains(
"Hello world"
)) {
JSONObject myLessonsresult3 = JSON.parseObject(result);
JSONArray catalogues = myLessonsresult3.getJSONArray(
"catalogue"
);
for
(
int
i3 =
0
; i3 < catalogues.size(); i3++) {
JSONObject catalogue = catalogues.getJSONObject(i3);
String catalogueName = catalogue.getString(
"name"
);
String current_filePath3 = current_filePath2 + (i3 <
9
? (
"0"
+ (i3 +
1
)) : (i3 +
1
))
+
" "
+ catalogueName +
"\\"
;
System.out.println(
"创建课程文件目录:"
+ current_filePath3);
new
File(current_filePath3).mkdirs();
JSONArray catalogues_childrens = catalogue.getJSONArray(
"children"
);
for
(
int
i4 =
0
; i4 < catalogues_childrens.size(); i4++) {
JSONObject catalogues_children = catalogues_childrens.getJSONObject(i4);
String catalogues_childrenName = catalogues_children.getString(
"name"
);
String current_filePath4 = current_filePath3 +
" "
+ (i4 <
9
? (
"0"
+ (i4 +
1
)) : (i4 +
1
)) + catalogues_childrenName +
"\\"
;
System.out.println(
"创建课程文件二级目录:"
+ current_filePath4);
new
File(current_filePath4).mkdirs();
JSONArray catalogues_childrens_too = catalogues_children.getJSONArray(
"children"
);
for
(
int
i5 =
0
; i5 < catalogues_childrens_too.size(); i5++) {
JSONObject kecheng = catalogues_childrens_too.getJSONObject(i5);
String sort = kecheng.getString(
"sort"
);
String playurl = url4 +
"?pkid="
+ LessonID +
"&id="
+ LessonID2 +
"&sid="
+ sort +
"&teach_type=2"
+
"&uid="
+ userid;
result = SdkClient.post(playurl,
""
, headMap);
if
(result !=
null
&& !
""
.equals(result) && result.contains(
"Hello world"
)) {
JSONObject fileinfo = JSON.parseObject(result);
fileinfo = fileinfo.getJSONObject(
"data"
);
String fileinfoName = fileinfo.getString(
"kj_name"
);
String fileinfoUrl = fileinfo.getString(
"aliInfo"
);
System.out.println(
"创建课程文件:"
+ current_filePath4 + fileinfoName);
System.out.println(fileinfoUrl);
File urltxt =
new
File(current_filePath4 +
"url.txt"
);
urltxt.createNewFile();
FileUtil.appendString(urltxt, fileinfoUrl);
tempmap.add(
new
String[] { fileinfoUrl, current_filePath4 + fileinfoName });
}
}
}
}
}
}
}
System.out.println(
"开启多线程下载"
);
ExecutorService threadPool = Executors.newCachedThreadPool();
int
threadCount =
5
;
int
threadTaskCount = tempmap.size() /
5
;
System.out.println(
"共开启"
+ threadCount +
"条线程,预计下载文件"
+ tempmap.size() +
"个,每条线程下载"
+ threadTaskCount +
"个文件"
);
CountDownLatch latch =
new
CountDownLatch(threadCount);
List<String[]> urls =
new
ArrayList();
for
(
int
iii =
0
; iii < tempmap.size(); iii++) {
String[] url = tempmap.get(iii);
urls.add(url);
if
(iii % threadTaskCount ==
0
|| iii == tempmap.size() -
1
) {
QueryTask task =
new
QueryTask(urls, latch);
threadPool.execute(task);
urls =
new
ArrayList();
}
}
latch.await();
threadPool.shutdown();
}
}
}
}
class
QueryTask
implements
Runnable {
private
List<String[]> urls;
private
CountDownLatch latc;
public
QueryTask() {
super
();
}
public
QueryTask(List<String[]> urls, CountDownLatch latc) {
super
();
this
.urls = urls;
this
.latc = latc;
}
public
static
boolean
download(String urlPath, String fileName)
throws
Exception {
URL url =
new
URL(urlPath);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setConnectTimeout(
3000
);
http.setRequestProperty(
"User-Agent"
,
"Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
);
InputStream inputStream = http.getInputStream();
byte
[] buff =
new
byte
[
1024
*
10
];
File file =
new
File(fileName);
if
(!file.exists()) {
OutputStream out =
new
FileOutputStream(file);
int
len;
int
count =
0
;
while
((len = inputStream.read(buff)) != -
1
) {
out.write(buff,
0
, len);
out.flush();
++count;
}
out.close();
inputStream.close();
http.disconnect();
return
true
;
}
return
false
;
}
@Override
public
void
run() {
try
{
for
(String[] u : urls) {
download(u[
0
], u[
1
]);
}
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
latc.countDown();
}
}
}