从 URL 的 Pdf、txt 或 docx 文件中解析文本,而无需在 Java 中下载它 8

Parse text from Pdf, txt, or docx file from URL without downloading it in Java 8

我需要能够使用给定的 url 在线解析文件中包含的文本,即 http://website.com/document.pdf

我正在做一个搜索引擎,基本上可以告诉我搜索的词是否在某个在线文件中,并检索文件的 URL,所以我不需要下载文件,只需阅读它。

我正在寻找一种方法,并找到了 InputStreamOpenConnection 的东西,但没能真正做到。

我正在使用 jsoup 在网站上爬行以检索 URLs,我试图用 Jsoup 方法解析它,但它不起作用。

那么最好的方法是什么?

编辑:

我希望能够做这样的事情:

File in = new File("http://website.com/document.pdf");
Document doc = Jsoup.parse(in, "UTF-8");
System.out.println(doc.toString());

您可以使用 URL 而不是文件来访问 URL。因此,使用 Apache Tika,您应该能够以这种方式获取内容的字符串。

import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

public class URLReader {
    public static void main(String[] args) throws Exception {

        URL url = new URL("http://website.com/document.pdf");
        ContentHandler contenthandler = new BodyContentHandler();
        Metadata metadata = new Metadata();
        PDFParser pdfparser = new PDFParser();
        pdfparser.parse(is, contenthandler, metadata, new ParseContext());

        System.out.println(contenthandler.toString());
    }
}

您可以使用此代码先下载 PDF 文件,然后使用 apache lib 阅读文本。你需要手动添加一些罐子。 您需要设置默认的本地 pdf 文件地址 "download.pdf".

import com.gnostice.pdfone.PdfDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;


public class LoadDocumentFromURL {
public static void main(String[] args) throws IOException {

        URL url1 = new URL("https://arxiv.org/pdf/1811.06933.pdf");
        byte[] ba1 = new byte[1024];
        int baLength;
        FileOutputStream fos1 = new FileOutputStream("download.pdf");
        try {
            // Contacting the URL
            // System.out.print("Connecting to " + url1.toString() + " ... ");
            URLConnection urlConn = url1.openConnection();
            // Checking whether the URL contains a PDF
            if (!urlConn.getContentType().equalsIgnoreCase("application/pdf")) {
                System.out.println("FAILED.\n[Sorry. This is not a PDF.]");
            } else {
                try {
                    // Read the PDF from the URL and save to a local file
                    InputStream is1 = url1.openStream();
                    while ((baLength = is1.read(ba1)) != -1) {
                        fos1.write(ba1, 0, baLength);
                    }
                    fos1.flush();
                    fos1.close();
                    is1.close();
                    // Load the PDF document and display its page count
                    //System.out.print("DONE.\nProcessing the PDF ... ");
                    PdfDocument doc = new PdfDocument();
                    try {
                        doc.load("download.pdf");
                        // System.out.println("DONE.\nNumber of pages in the PDF is " + doc.getPageCount());
                        // System.out.println(doc.getAuthor());
                        // System.out.println(doc.getKeywords());
                        // System.out.println(doc.toString());
                        doc.close();
                    } catch (Exception e) {
                        System.out.println("FAILED.\n[" + e.getMessage() + "]");
                    }
                } catch (ConnectException ce) {
                    //System.out.println("FAILED.\n[" + ce.getMessage() + "]\n");
                }
            }
        } catch (NullPointerException npe) {
            //System.out.println("FAILED.\n[" + npe.getMessage() + "]\n");
        }
        File file = new File("your local pdf file address which is download.pdf");
        PDDocument document = PDDocument.load(file);
        PDFTextStripper pdfStripper = new PDFTextStripper();
        String text = pdfStripper.getText(document);
        System.out.println(text);
        document.close();
    }
}