java class 如何在 WebLogic 12c 中定位并加载静态 pdf 文件?

How can a java class locate and load a static pdf file in WebLogic 12c?

我需要在 WebLogic 12c 上的 J2EE Web 应用程序中从 java class、运行ning 加载静态 PDF 文档;然而,尽管我的代码在 Tomcat 中有效,但当我尝试在 WebLogic 12c(WebLogic Server 版本:12.2.1.2.0)中对它进行 运行 时,我收到一个服务器错误,指出无法找到 PDF 文件(java.io.FileNotFoundException).

我正在使用 Apache 的 PDF 库 PDFBox 2.0.8 版加载我创建的可填写 PDF 文件,然后用数据填充该可填写 PDF。我的代码在 Tomcat 中运行良好,但在部署到 WebLogic 12c 时无法找到 pdf 文件。

-这似乎是因为当一个 EAR 文件部署到 WebLogic 12c 时,WAR 文件中的内容(所有应用程序 code/files,包括可填写的 PDF 文件)仍然存在存档在 WebLogic 创建的 jar 文件中,而不是展开。

我的应用程序使用标准的 Maven 应用程序结构,因此作为所有静态文件的标准,我将我的 PDF 文件放在静态资源目录中:

src/main/resources/

在我的 pom.xml 文件中,我有以下内容,它将 /src/main/resources/ 文件夹中的任何 pdf 文件构建到 [=61 的 class 路径根文件夹中=] 文件.

<resource>
    <directory>${basedir}/src/main/resources/</directory>
        <includes>
        <include>**/*.xml</include>
        <include>**/*.properties</include>
       <include>**/*.pdf</include>
    </includes>
</resource>

当我构建 WAR 和 EAR 文件时,pdf 文件确实被复制到应用程序 class 文件的根文件夹中。

以下 3 行代码,用于加载 PDF,当我的应用程序的 EAR 文件部署在 Tomcat 中时,但不在 WebLogic 12c(WebLogic Server 版本:12.2.1.2.0)中。

//this classLoader works for Tomcat, but no in WebLogic 12c
ClassLoader classLoader = getClass().getClassLoader();
File file= new File(classLoader.getResource("myPdfFile.pdf").getFile());
PDDocument document = PDDocument.load(file);

WebLogic 12c produces the following error:

<[ACTIVE] ExecuteThread: '6' for queue:

'weblogic.kernel.Default (self-tuning)'> <> <> <3902331f-a214-42fe-a6a1-35b3531e4b56-000000a9> <1523985710798> <[severity-value: 8] [rid: 0] [partition-id: 0] [partition-name: DOMAIN] > <[ServletContext@1661988196[app:mmhsrp-ear-4.9.0.1-3 module:/mmhsrp path:null spec-version:3.1]] Servlet failed with an IOException. java.io.FileNotFoundException: C:\Users\shawn.oplinger\AppData\Roaming\JDeveloper\system12.2.1.2.42.161008.1648\DefaultDomain\servers\DefaultServer\tmp_WL_user\mmhsrp-ear-4.9.0.1-3\l27tj7\war\WEB-INF\lib_wl_cls_gen.jar!\myPdfFile.pdf (The system cannot find the path specified)

-When I browse my file system, to manually try to find the pdf file using the path in the WebLogic I can get as far as this directory: C:\Users\shawn.oplinger\AppData\Roaming\JDeveloper\system12.2.1.2.42.161008.1648\DefaultDomain\servers\DefaultServer\tmp_WL_user\mmhsrp-ear-4.9.0.1-3\l27tj7\war\WEB-INF\lib\

在该目录中就是这个文件: _wl_cls_gen.jar

那个jar文件里确实是我的pdffile:myPdfFile.pdf

-那么为什么 classLoader 不能 find/load pdf 文件?

-与pdf文件实际上在归档_wl_cls_gen.jar文件中,而不是展开有什么关系吗?

-关于我的 java class 如何在 WebLogic 12c 中加载静态 pdf 文件有什么建议吗?

谢谢! 肖恩

您的代码

//this classLoader works for Tomcat, but no in WebLogic 12c
ClassLoader classLoader = getClass().getClassLoader();
File file= new File(classLoader.getResource("myPdfFile.pdf").getFile());
PDDocument document = PDDocument.load(file);

假定资源在文件系统中作为单个文件可用。正如您的错误消息和您的搜索显示的那样,WebLogic 中的情况并非如此,而是包含在一个 jar 文件中。

因此,您的代码不应尝试通过 File 对象访问它。相反,通过 InputStream 访问是可能的,并且 PDFBox 也支持:

InputStream resource = classLoader.getResourceAsStream("myPdfFile.pdf");
PDDocument document = PDDocument.load(resource);
resource.close();