gpt4 book ai didi

java - 用 Java 下载 MP4 文件?

转载 作者:行者123 更新时间:2023-12-01 19:01:59 25 4
gpt4 key购买 nike

我正在尝试创建一个 java 程序,使用 JSoup 从特定网站下载视频。但是,我现在意识到 JSoup 无法下载 mp4 文件;他们不受支持。目前,我拥有它,因此它连接到网站,获取 IFrame 链接,该链接获取存储在另一个网站上的下载链接,然后从服务器获取直接下载链接。现在,我不知道如何实际下载该文件。我对 Java 还很陌生,所以我不知道如何使用 Https 请求或 File.IO 实用程序以这种方式下载文件。我查了一些例子,但似乎没有一个适合我。我还尝试使用 Apache Commons IO,但无法将其添加为库;每当我转到“添加文件或目录”时,.Java 文件都不会显示(我也尝试了“选择库类型”),也不允许我拖放文件。无论如何,我不知道还能做什么,所以我发布了这个。任何帮助将不胜感激,谢谢!

这是我当前的代码:

package com.trentmenard;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;


class ExtractorAndDownloader {
private String URL;
private Document currentWebsite;

ExtractorAndDownloader(String URL) {
this.URL = URL;
getIFrameLink(URL);
}

private void getIFrameLink(String URL) {
System.out.println("Attempting Connection to: " + URL);
try {
currentWebsite = Jsoup.connect(URL).get();
System.out.println("Connection Successfully Established!");
Element iFrame = currentWebsite.selectFirst("iframe");
String iFrameLink = iFrame.attr("src");
System.out.println("Grabbing IFrame Link: " + iFrameLink);
getDownloadLink(iFrameLink);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Connection Failed!");
}
}

private void getDownloadLink(String URL) {
System.out.println("Attempting Connection to: " + URL);
try {
currentWebsite = Jsoup.connect(URL).get();
System.out.println("Connection Successfully Established!");
Element hiddenElementID = currentWebsite.getElementById("id");
String hiddenElementIDValue = hiddenElementID.attr("value");
System.out.println("Grabbing Hidden Element ID Value: " + hiddenElementIDValue);
System.out.println("Grabbing ReDirect Link: " + " https://www.vidstreaming.io/download?id=" + hiddenElementIDValue);
getReDirectDownloadLink("https://www.vidstreaming.io/download?id=" + hiddenElementIDValue);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Connection Failed!");
}
}

private void getReDirectDownloadLink(String URL) {
System.out.println("Attempting Connection to: " + URL);
try {
currentWebsite = Jsoup.connect(URL).get();
System.out.println("Connection Successfully Established!");
Element dowloadClassOriginal = currentWebsite.getElementsContainingOwnText("Download (orginalP - mp4)").first();
String dowloadClassOriginalReDirectLink = dowloadClassOriginal.attr("href");
System.out.println("Grabbing ReDirect Link: " + dowloadClassOriginalReDirectLink);
downloadUsingDirectLink(dowloadClassOriginalReDirectLink);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Connection Failed!");
}
}

private void downloadUsingDirectLink(String URL) {
System.out.println("Attempting Connection to: " + URL);
try{
currentWebsite = Jsoup.connect(URL).get();
System.out.println("Connection Successfully Established!");
}
catch (IOException e){
e.printStackTrace();
System.out.println("Connection Failed!");
}

}
}

这是错误消息,指出这是一个不受支持的文件:

"C:\Program Files\Java\jdk1.8.0_201\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\lib\idea_rt.jar=61448:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;C:\Users\Trent\Desktop\SAOEpisodeDownloader\out\production\SAOEpisodeDownloader;C:\Users\Trent\Downloads\jsoup-1.12.1.jar" com.trentmenard.Main
Attempting Connection to: https://swordartonlineepisode.com/sword-art-online-season-3-episode-1-english-dubbed-watch-online/
Connection Successfully Established!
Grabbing IFrame Link: https://vidstreaming.io/streaming.php?id=MTEzMzQ1
Attempting Connection to: https://vidstreaming.io/streaming.php?id=MTEzMzQ1
Connection Successfully Established!
Grabbing Hidden Element ID Value: MTEzMzQ1
Grabbing ReDirect Link: https://www.vidstreaming.io/download?id=MTEzMzQ1
Attempting Connection to: https://www.vidstreaming.io/download?id=MTEzMzQ1
Connection Successfully Established!
Grabbing ReDirect Link: https://st1x.cdnfile.info/user1342/1f1b04321e51fac1c1d1c23a1de4f7f0/EP.1.mp4?token=QPVm8x5GnYo6uFYLHqfjwg&expires=1578391941&id=113345&title=(orginalP - mp4) Sword+Art+Online%3A+Alicization+%28Dub%29+Episode+1
Attempting Connection to: https://st1x.cdnfile.info/user1342/1f1b04321e51fac1c1d1c23a1de4f7f0/EP.1.mp4?token=QPVm8x5GnYo6uFYLHqfjwg&expires=1578391941&id=113345&title=(orginalP - mp4) Sword+Art+Online%3A+Alicization+%28Dub%29+Episode+1
org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=video/mp4, URL=https://st1x.cdnfile.info/user1342/1f1b04321e51fac1c1d1c23a1de4f7f0/EP.1.mp4?token=QPVm8x5GnYo6uFYLHqfjwg&expires=1578391941&id=113345&title=(orginalP%20-%20mp4)%20Sword+Art+Online%3A+Alicization+%28Dub%29+Episode+1
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:770)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:705)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:295)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:284)
at com.trentmenard.ExtractorAndDownloader.downloadUsingDirectLink(ExtractorAndDownloader.java:68)
at com.trentmenard.ExtractorAndDownloader.getReDirectDownloadLink(ExtractorAndDownloader.java:58)
at com.trentmenard.ExtractorAndDownloader.getDownloadLink(ExtractorAndDownloader.java:43)
at com.trentmenard.ExtractorAndDownloader.getIFrameLink(ExtractorAndDownloader.java:27)
at com.trentmenard.ExtractorAndDownloader.<init>(ExtractorAndDownloader.java:16)
at com.trentmenard.Main.main(Main.java:7)
Connection Failed!

Process finished with exit code 0

最佳答案

你可以试试这个

private static void getVideo(String src) throws IOException {

String folder = null;

//Exctract the name of the video from the src attribute
int indexname = src.lastIndexOf("/");

if (indexname == src.length()) {
src = src.substring(1, indexname);
}

indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());

System.out.println(name);

//Open a URL Stream
URL url = new URL(src);
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream( folderPath+ name));
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}

来源:https://examples.javacodegeeks.com/enterprise-java/html/download-images-from-a-website-using-jsoup/

关于java - 用 Java 下载 MP4 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59623263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com