3

您好我想使用 Apache POI 库向网页显示 Microsoft 文档内容

这作为 java 应用程序运行良好。

但是应用程序在jsp上抛出异常

附言

以下是我到目前为止的工作

<%@ page import="java.io.*" %>
<%@ page import="org.apache.poi.openxml4j.opc.OPCPackage" %>
<%@ page import="org.apache.poi.xwpf.extractor.XWPFWordExtractor" %>
<%@ page import="org.apache.poi.xwpf.usermodel.XWPFDocument" %>
<body>
<form name="form1" method="get" action="FileServlet"
    style="height: 78px;">
    <input type="file" name="xls_filename" align="top"
        onchange="form1.submit()">
</form>
<%
    String filename = request.getParameter("xls_filename");

    if (filename != null && !filename.equals("")) {
%>
<br>You chose the file
<%=filename%>.

<br>It's contents are:
<%
    InputStream fs = null;
        String fileData;

        XWPFWordExtractor extractor = null;

        try {
            fs = new FileInputStream(filename);

            XWPFDocument hdoc = new XWPFDocument(OPCPackage.open(fs));

            extractor = new XWPFWordExtractor(hdoc);

            fileData = extractor.getText();
            System.out.println(fileData);
            // document.add(new Paragraph(fileData));
            System.out.println(" pdf document created");
        } catch (IOException e) {
            System.out.println("IO Exception");
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            //  document.close();
        }
%>
<%
    }

%>
</body>

堆栈跟踪:

 at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

以上是完整的堆栈跟踪和错误显示为:

在 jsp 文件中的第 40 行发生错误:/index.jsp XWPFDocument 无法解析为类型

在 jsp 文件中的第 40 行出现错误:/index.jsp OPCPackage 无法解析

在 jsp 文件中的第 42 行发生错误:/index.jsp XWPFWordExtractor 无法解析为类型

4

2 回答 2

3

Jar 库未正确添加到类路径中

dom4j-1.6.1.jar、xmlbeans-2.3.0.jar、
poi-3.9-20121203.jar、poi.ooxml-3.9-20121203.jar、poi.ooxml-schemas-3.9-20121203.jar、

放在 jar 上方的 lib

为我工作

感谢您的帮助

于 2013-06-20T10:06:29.493 回答
2

您需要确保在您的 jsp 中添加导入,并在 WEB-INF/lib 中有所有需要的 jar

<%@ page import="java.io.*" %>
<%@ page import="org.apache.poi.xwpf.extractor.*" %>
<%@ page import="org.apache.poi.xwpf.usermodel.*" %>
<%@ page import="org.apache.poi.openxml4j.opc.*" %>
<%@ page import="org.apache.xmlbeans.*" %>
于 2013-06-20T08:47:24.083 回答