【问题标题】: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,我必须从中提取数据并显示,但我得到了这个异常,我无法弄清楚这个异常是什么?
【发布时间】:2015-06-08 10:54:48
【问题描述】:

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

我的代码 sn-p:

<%
                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)

【问题讨论】:

  • 它只是说你的程序找不到指定的文件。看看你是否正确引用了你的文件,它们是否在类路径中。
  • 嘿,@Aakash,我实际上添加了代码 sn-p,我不知道为什么它没有显示在这里?
  • 您应该使用过代码格式。没关系,我已经编辑过了。
  • 非常感谢@Aakash 的编辑

标签: java exception servlets itext pdf-reader


【解决方案1】:

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

使用类加载器...

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

或者您可以将其作为 url 获取

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多