我有一个 pdf,我必须从中提取数据并显示,但我遇到了这个异常,我无法弄清楚这个异常是什么?

I have a pdf from which I have to extract data and show but I am getting this exception, I'm not being able to figure out what is this Exception is?

我有这个 pdf 文件可以从中提取数据,但问题是当我 运行 将这段代码作为一个简单的 java 程序时,它的工作就像一个魅力但是当我在 JSP servlet 上尝试了这段代码,它给出了这个异常,我无法弄清楚这个异常是干什么用的? 我正在使用 iText API 从 PDF 中提取文本。

我的代码片段:

<%
                String Source = "Resource/text.pdf";

                PdfReader reader;
                try {
                    //java.net.URL SourcePDF = getClass().getResource("RawMaterial/text.pdf");
                    reader = new PdfReader(Source);

                    PdfReaderContentParser parser = new PdfReaderContentParser(reader);

                    TextExtractionStrategy strategy = null;

                    for (int i = 1; i <= reader.getNumberOfPages(); i++) 
                    {
                        strategy = parser.processContent(i,new SimpleTextExtractionStrategy());
                        System.out.println(strategy.getResultantText());
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            %>

例外情况是:

java.io.IOException: Resource/text.pdf not found as file or resource.
    at com.itextpdf.text.io.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:263)
    at com.itextpdf.text.io.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:173)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:219)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)
    at org.apache.jsp.test_jsp._jspService(test_jsp.java:120)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

找不到您的文件,因为当打包为 war/jar 或您选择的任何文件时,它可能位于不同的位置。检查您的 war 文件,您可能会在 /classes/some/package 下找到它。所以在这种情况下,将其作为具有相对路径的资源来读取。

使用类加载器...

ClassLoader.getResourceAsStream ("your/pkg/Resource/text.pdf");

或者您可以将其作为 url

URL url = getClass().getClassLoader().getResource("your/pkg/Resource/text.pdf");

终于,问题解决了,我只是在开头添加了这一行代码,在指定路径之前 -

<% 
    String relativePath = getServletContext().getRealPath("/");
    String Source = relativePath +"Resource/text.pdf";
    ...
    ...
%>