Наконец-то я смог обнаружить пустые веб-страницы в зависимости от их размера (ниже 3 КБ) и сканирования содержимого. Сначала я загружаю контент HTML через URLConnection
и сохраняю его в памяти.
public ArrayList<String> captureHtmlTags(String inputUrl) {
ArrayList<String> readWebsiteHTMLTags = new ArrayList<String>();
try {
URL url = new URL(inputUrl);
try {
URLConnection connection = url.openConnection();
// open the stream and assign it into buffered reader..
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
if (bufferedReader.readLine() != null) {
while ((inputLine = bufferedReader.readLine()) != null) {
Document doc = Jsoup.parse(inputLine);
readWebsiteHTMLTags.add(String.valueOf(doc));
}
bufferedReader.close();
readWebsiteHTMLTags.size();
} else {
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return readWebsiteHTMLTags;
}
Затем, используя библиотеку iText
7, я конвертирую чтение HTML в PDF и сканирую текстовое содержимое.
public String convertWebsiteToPdf(ArrayList<String> htmlCode) {
String generateHtmlPage = null;
String fileLocation = CONST_FILE_PATH; // path to save the PDFs
generateHtmlPage = htmlCode.toString();
FileOutputStream fileOutputStream = null;
try {
try {
fileOutputStream = new FileOutputStream(fileLocation);
HtmlConverter.convertToPdf(generateHtmlPage, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return fileLocation;
}
Если содержимое не найдено вместе с небольшим размером файла, я считаю его пустой веб-страницей.
Наконец-то это сработало.