gpt4 book ai didi

java - 是什么导致我的代码循环?错误地实例化了一个类?

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

我正在开发一个程序,该程序从链接中获取 Iframe,然后继续下载该剧集。由于某种原因,当我调用该方法来实际下载位于另一个类中的文件时,它会导致无限循环并生成更多该类的实例,从而导致程序不断创建新的 JFrame。可能是什么原因造成的?我没有正确实例化该类吗?

第 21 行是调用另一个类的地方,从而导致循环。

第 1 类:

package com.trentmenard;
import org.jsoup.*;
import org.jsoup.nodes.*;
import java.io.IOException;

class WebsiteScraper {
private String URL;
private Document websiteConnection;
private String episodeName;
private String iFrameLink;
private String URLDownloadLink;

//FOR DEBUG PURPOSES:
private String directDownloadLink = "https://st1x.cdnfile.info/user1342/1f1b04321e51fac1c1d1c23a1de4f7f0/" +
"EP.1.mp4?token=qV-3haWjcomPzXJVAeBtdg&expires=1579170124&id=113345&title=(orginalP - mp4) Sword+Art+" +
"Online%3A+Alicization+%28Dub%29+Episode+1";

WebsiteScraper(String URL) {
this.URL = URL;
//connectToURL(URL);
new DownloadEpisode(directDownloadLink, episodeName); //LOOP STARTS HERE
}
private void connectToURL(String URL){
if(URL.startsWith("https://st1x.cdnfile.info/")){
getDirectDownloadLink();
}
else{
try {
websiteConnection = Jsoup.connect(URL).get();
System.out.println("Connection Successfully Established to: " + URL);

if(URL.equalsIgnoreCase("https://swordartonlineepisode.com/sword-art-online-season-3-episode" +
"-1-english-dubbed-watch-online/")){
episodeName = "Sword Art Online Season 3 Episode 1 English Dubbed";
getIFrameLink();
}
else if((URL).equalsIgnoreCase(iFrameLink)) {
getDownloadLink();
}
else if(URL.equalsIgnoreCase(URLDownloadLink)){
getDirectDownloadLink();
}
}
catch (IOException e){
e.printStackTrace();
System.out.println("Failed to Establish Connection to:" + URL);
}
}
}

String getNextEpisodeURL(){
String nextEpisodeURL = websiteConnection.getElementsByClass("button SAO").get(1).attr("href");
System.out.println("Found Next Episode URL: " + nextEpisodeURL);
return nextEpisodeURL;
}

String getNextEpisodeName(){
Element h2 = websiteConnection.selectFirst("h2");
String nextEpisodeName = h2.text();
System.out.println("Found Next Episode URL: " + nextEpisodeName);
//getIFrameLink();
return nextEpisodeName;
}

private void getIFrameLink(){
Element iFrame = websiteConnection.selectFirst("iframe");
iFrameLink = iFrame.attr("src");
System.out.println("Found iFrame Link: " + iFrameLink + " for: " + episodeName);
connectToURL(iFrameLink);
}


private void getDownloadLink() {
Element hiddenID = websiteConnection.getElementById("id");
String hiddenIDValue = hiddenID.attr("value");
URLDownloadLink = "https://www.vidstreaming.io/download?id=" + hiddenIDValue;
System.out.println("Found Download Link Using ID Value (" + hiddenIDValue + "): " + URLDownloadLink);
connectToURL(URLDownloadLink);
}

private void getDirectDownloadLink(){
if(URL.equalsIgnoreCase(URLDownloadLink)) {
new DownloadEpisode(URL, episodeName);
}
else if(URL.startsWith("https://st1x.cdnfile.info/")){
new DownloadEpisode(URL, episodeName);
}
else{
Element downloadClass = websiteConnection.getElementsContainingOwnText("Download (orginalP - mp4)").first();
directDownloadLink = downloadClass.attr("href");
System.out.println("Found Direct Download Link: " + directDownloadLink);
new DownloadEpisode(directDownloadLink, episodeName);
}
}
}

第 2 类:

package com.trentmenard;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;

import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

class DownloadEpisode{

private String URL;
private static DecimalFormat decimalFormat = new DecimalFormat("#.##");

public DownloadEpisode(String URL, String episodeName) {
System.out.println("[Debug:] Instance called!");
this.URL = URL;

float Percent = 0;
String downloadProgress = "0.00";

JFrame progressFrame = new JFrame();
JProgressBar progressBar = new JProgressBar(0, 100);

progressBar.setSize(100, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);

progressFrame.setTitle("Downloading: " + episodeName + " - " + Percent + "%");
progressFrame.add(progressBar);
progressFrame.setVisible(true);
progressFrame.setLayout(new FlowLayout());
progressFrame.setSize(575, 100);
progressFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

JLabel percentComplete = new JLabel(downloadProgress + "% complete.");
progressFrame.add(percentComplete);

WebsiteScraper websiteScraper = new WebsiteScraper(URL);
String nextEpisodeURl = websiteScraper.getNextEpisodeURL();
String nextEpisodeName = websiteScraper.getNextEpisodeName();

File createFile = new File(episodeName + ".mp4");
if(createFile.exists() && !createFile.isDirectory()) {
System.out.println("File Already Exists! Moving Onto Next URL.");
new DownloadEpisode(nextEpisodeURl, nextEpisodeName);
}
else{
try {
java.net.URL url = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

System.out.println("Connection Successfully Established!");
System.out.println("Downloading File: " + episodeName);

int filesize = connection.getContentLength();
float totalDataRead = 0;
byte[] data = new byte[1024];
int i = 0;

java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(episodeName + ".mp4");
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);

while ((i = in.read(data, 0, 1024)) >= 0) {
totalDataRead = totalDataRead + i;
bout.write(data, 0, i);
Percent = (totalDataRead * 100) / filesize;
decimalFormat.setRoundingMode(RoundingMode.CEILING);
downloadProgress = decimalFormat.format(Percent);

progressFrame.setTitle("Downloading: " + episodeName);
progressBar.setValue((int) Percent);
percentComplete.setText(downloadProgress);
}
bout.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Connection Failed!");
}
}
}
}

/image/x1c5A.png

最佳答案

您的WebsiteScraper正在创建一个DownloadEpisode,它在调用DownloadEpisode的构造函数中创建一个WebsiteScraper并且等等。

如果您在创建类时遇到排序问题,请查看工厂模式。

关于java - 是什么导致我的代码循环?错误地实例化了一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59779529/

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