好友
阅读权限 10
听众
最后登录 1970-1-1
final static String fileSrc = "F:" + File.separator + "down" + File.separator ;
public static void main (String[] args) {
// 文件地址
String fileSrc = "F:" + File.separator + "360download" + File.separator ;
// 文件名称
String fileSuffix = "3.txt" ;
// 将批量准备下载的链接放入 txt 文件
// 格式为 : 文件名 下载链接
readFileByLine (fileSrc, fileSuffix);
// 单个下载的链接
dowVedio ("test.mp4" , "https://www.douyin.com/video/7209110067788942649" );
}
public static boolean isInteger (String str) {
Pattern pattern = Pattern.compile (" ^[- \\ +]?[ \\ d]*$ " );
return pattern.matcher(str).matches();
}
/**
* fileSrc = " 盘符 "+" 存放路径文件夹 "
* @Param buffer
* @param num
* @return
*/
public static boolean isExists (StringBuffer buffer, StringBuffer num) {
String src = " 【 " + num.toString() + " 】 " + buffer.toString().substring(buffer.toString().indexOf("*" ) + 1 , buffer.toString().indexOf(" " ));
String vedioSuffix = ".mp4" ;
if (new File(fileSrc + src + vedioSuffix).exists()) {
System.out .println(" 文件已存在 " );
return true;
}
return false;
}
/**
* 按行读取文件
*
* @param
*/
public static void readFileByLine (String fileSrc, String fileSuffix) {
ExecutorService fixedThreadPool = Executors.newFixedThreadPool (300 );
try {
File file = new File(fileSrc + fileSuffix);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String strLine = null;
StringBuffer buffer = new StringBuffer();
StringBuffer num = new StringBuffer();
int lineCount = 1 ;
while (null != (strLine = bufferedReader.readLine())) {
// System.out.println(" 第 [" + lineCount + "] 行数据 :[" + strLine + "]");
if (strLine.contains(" \\ " )) {
if (isInteger (strLine.substring(1 ))) {
num.append(strLine.substring(1 ));
}
}
if (strLine.contains("*" )) {
buffer.append(strLine);
if (isExists (buffer, num)) {
//break; 结束所有循环
num.delete(0 , num.length());
buffer.delete(0 , buffer.length());
continue; // 立即结束本次循环,继续执行下一次循环。
}
Thread.sleep (9000 );
fixedThreadPool.execute(new Runnable() {
@Override
public void run () {
//do something
String OldUrl = buffer .toString();
int httpOne = buffer .toString().indexOf("h" );
int httpTwo = buffer .toString().lastIndexOf("/" );
String src = " 【 " + num + " 】 " + buffer .toString().substring(buffer .toString().indexOf("*" ) + 1 , buffer .toString().indexOf("%" ));
src = src.replace("." , "" )
.replace("…" , "" )
.replace("“" , "" )
.replace(" \" " , "" )
.replace(" ! " , "" )
.replace(" ? " , "" )
.replace(":" , "" )
.replace("/" , "" );
String vedioSuffix = ".mp4" ;
System.out .println(" 正在保存 :" + src + " 正极 :" + httpOne + " 负极 :" + httpTwo);
dowVedio (fileSrc + src + vedioSuffix,
OldUrl.substring(httpOne, httpTwo));
// System.out.println("mn");
// System.out.println(buffer.toString().substring(buffer.toString().indexOf("h"),buffer.toString().indexOf(" 复 ")));
}
});
Thread.sleep (1000 );
num.delete(0 , num.length());
buffer.delete(0 , buffer.length());
// System.out.println(strLine.substring(strLine.indexOf("h"),strLine.indexOf(" 复 ")));
}
lineCount++;
}
fixedThreadPool.shutdown(); // 关闭线程池
} catch (Exception e) {
e.printStackTrace();
}
}
public static void getFile (InputStream is, String fileName) throws IOException {
BufferedInputStream in = null;
BufferedOutputStream out = null;
in = new BufferedInputStream(is);
out = new BufferedOutputStream(Files.newOutputStream (Paths.get (fileName)));
int len = -1 ;
byte [] b = new byte [1024 ];
while ((len = in.read(b)) != -1 ) {
out.write(b, 0 , len);
}
in.close();
out.close();
}
public static String dowVedio (String saveFileName, String urlStr) {
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
try {
String dowUrl = getDowUrl (urlStr);
URL url = new URL(dowUrl);
urlConnection = (HttpURLConnection) url.openConnection();
//Post Request Define:
urlConnection.setRequestMethod("GET" );
urlConnection.connect();
inputStream = urlConnection.getInputStream();
getFile (inputStream, saveFileName);
} catch (Exception e) {
throw new RuntimeException(" 下载失败 !" );
} finally {
try {
if (inputStream != null ) {
inputStream.close();
}
if (urlConnection != null ) {
urlConnection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
return " 成功 !" ;
}
}
public static String getDowUrl (String h) {
//https://v.douyin.com/JogG5tc/ https%3A//v.douyin.com/Joguhrm/";
String param = "?url=" ;
String http = h.replaceAll(" : " , "%3A" ).trim();
String r = "&r=" + SnowflakeIdWorker.generateId ().toString().substring(0 , 16 );
String s = "&s=" + UUID.randomUUID ().toString().replaceAll(" - " , "" );
String urls = "https://3g.gljlw.com/diy/douyin.php" + param + http + r + s;
String downloadURL = null;
try {
URL url = new URL(urls);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//Post Request Define:
urlConnection.setRequestMethod("GET" );
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
String responseStr = ConvertToString (inputStream);
String elemStart = "<textarea name= \" book_content \" class= \" KL_bbs_textarea \" rows= \" 5 \" style= \" width:100% \" >" ;
String elemEnd = "</textarea>" ;
int indexa = responseStr.indexOf(elemStart);
int indexb = responseStr.indexOf(elemEnd);
downloadURL = responseStr.substring(indexa + elemStart.length(), indexb);
} catch (Exception e) {
e.printStackTrace();
}
return downloadURL;
}
public static String ConvertToString (InputStream inputStream) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder result = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null ) {
result.append(line).append(" \n " );
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStreamReader.close();
inputStream.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result.toString();
}
免费评分
查看全部评分
发帖前要善用【论坛搜索 】 功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。