kt携程版本

This commit is contained in:
lushunming 2025-07-11 11:33:10 +08:00
parent 2a0682ea22
commit 191d961f84
4 changed files with 3 additions and 115 deletions

View File

@ -11,8 +11,6 @@ import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
@ -20,10 +18,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import okhttp3.Response; import okhttp3.Response;
@ -103,113 +97,7 @@ public class ProxyVideo {
} }
public static Object[] proxyMultiThread(String url, Map<String, String> headers) { public static Object[] proxyMultiThread(String url, Map<String, String> headers) {
ExecutorService service = Executors.newFixedThreadPool(THREAD_NUM); return DownloadMT.INSTANCE.proxyMultiThread(url, headers);
SpiderDebug.log("--proxyMultiThread: THREAD_NUM " + THREAD_NUM);
SequenceInputStream in = null;
try {
//缓存避免每次都请求total等信息
Object[] info = infos.get(url);
if (info == null) {
infos.clear();
info = getInfo(url, headers);
infos.put(url, info);
}
int code = (int) info[0];
if (code != 206) {
return proxy(url, headers);
}
Map<String, String> resHeader = (Map<String, String>) info[3];
String contentRange = StringUtils.isAllBlank(resHeader.get("Content-Range")) ? resHeader.get("content-range") : resHeader.get("Content-Range");
SpiderDebug.log("--contentRange:" + contentRange);
//文件总大小
String total = StringUtils.split(contentRange, "/")[1];
SpiderDebug.log("--文件总大小:" + total);
//如果文件太小也不走代理
if (Long.parseLong(total) < 1024 * 1024 * 100) {
return proxy(url, headers);
}
String range = StringUtils.isAllBlank(headers.get("range")) ? headers.get("Range") : headers.get("range");
SpiderDebug.log("---proxyMultiThread,Range:" + range);
//没有range,强行给他一个range让他去分片
if (StringUtils.isAllBlank(range)) {
range = "bytes=0-";
}
Map<String, String> rangeObj = parseRange(range);
List<long[]> partList = generatePart(rangeObj, total);
// 存储执行结果的List
List<Future<Response>> results = new ArrayList<>();
for (long[] part : partList) {
String newRange = "bytes=" + part[0] + "-" + part[1];
SpiderDebug.log("下载开始" + ";newRange:" + newRange);
Map<String, String> headerNew = new HashMap<>(headers);
headerNew.put("range", newRange);
headerNew.put("Range", newRange);
Future<Response> result = service.submit(() -> {
try {
return OkHttp.newCall(url, headerNew);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
results.add(result);
}
List<InputStream> inputStreams = new ArrayList<>();
for (int i = 0; i < THREAD_NUM; i++) {
// 获取包含返回结果的future对象
Future<Response> future = results.get(i);
// 从future中取出执行结果若尚未返回结果则get方法被阻塞直到结果被返回为止
Response response = future.get();
okhttp3.ResponseBody body = response.body();
/* int bytesRead;
while ((bytesRead = body.byteStream().read(bytes)) != -1) {
out.write(bytes, 0, bytesRead);
}*/
inputStreams.add(body.byteStream());
SpiderDebug.log("---第" + i + "块下载完成" + ";Content-Range:" + response.headers().get("Content-Range"));
SpiderDebug.log("---第" + i + "块下载完成" + ";content-range:" + response.headers().get("content-range"));
}
in = new SequenceInputStream(new Vector<>(inputStreams).elements());
service.shutdown();
String contentType = StringUtils.isAllBlank(resHeader.get("Content-Type")) ? resHeader.get("content-type") : resHeader.get("Content-Type");
String contentDisposition = resHeader.get("Content-Disposition");
if (contentDisposition != null && StringUtils.isAllBlank(contentType)) {
contentType = getMimeType(contentDisposition);
}
/* respHeaders.put("Access-Control-Allow-Credentials", "true");
respHeaders.put("Access-Control-Allow-Origin", "*");*/
resHeader.put("Content-Length", String.valueOf(partList.get(THREAD_NUM - 1)[1] - partList.get(0)[0] + 1));
// respHeaders.put("content-length", String.valueOf(bytes.length));
resHeader.put("Content-Range", String.format("bytes %s-%s/%s", partList.get(0)[0], partList.get(THREAD_NUM - 1)[1], total));
// respHeaders.put("content-range", String.format("bytes %s-%s/%s", partList.get(0)[0], partList.get(THREAD_NUM - 1)[1], total));
SpiderDebug.log("++proxy res contentType:" + contentType);
// SpiderDebug.log("++proxy res body:" + response.body());
SpiderDebug.log("++proxy res respHeaders:" + Json.toJson(resHeader));
return new Object[]{206, contentType, in, resHeader};
} catch (Exception e) {
SpiderDebug.log("proxyMultiThread error:" + e.getMessage());
e.printStackTrace();
return null;
}
} }
public static List<long[]> generatePart(Map<String, String> rangeObj, String total) { public static List<long[]> generatePart(Map<String, String> rangeObj, String total) {

Binary file not shown.

View File

@ -1 +1 @@
0cab1186330902b80785825911735a7d 860afdcd1a39fd5c0bcfae2b0652ec7c

View File

@ -1,5 +1,5 @@
{ {
"spider": "https://gh-proxy.com/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThread/jar/custom_spider.jar;md5;2f2f43db60e7493b3af54c85a8172218", "spider": "https://gh-proxy.com/https://raw.githubusercontent.com/lushunming/AndroidCatVodSpider/multiThreadkt/jar/custom_spider.jar;md5;860afdcd1a39fd5c0bcfae2b0652ec7c",
"lives": [ "lives": [
{ {
"name": "电视直播", "name": "电视直播",