Sha*_*ger 1 pdfbox weblogic12c
我需要从 java 类加载一个静态 PDF 文档,该文档在 WebLogic 12c 上的 J2EE Web 应用程序中运行;然而,尽管我的代码可以在 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 文件中的内容(所有应用程序代码/文件,包括可填写的 PDF 文件)仍然存档在 WebLogic 创建的 jar 文件中,而不是爆炸了。
我的应用程序使用标准的 Maven 应用程序结构,因此与所有静态文件都是标准的,我已将 PDF 文件放在静态资源目录中:
src/主/资源/
在我的 pom.xml 文件中,我有以下内容,它将 /src/main/resources/ 文件夹中的所有 pdf 文件构建到 WAR 文件的类路径根文件夹中。
<resource>
<directory>${basedir}/src/main/resources/</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.pdf</include>
</includes>
</resource>
Run Code Online (Sandbox Code Playgroud)
当我构建 WAR 和 EAR 文件时,pdf 文件确实被复制到应用程序类文件的根文件夹中。
当我的应用程序的 EAR 文件部署在 Tomcat 中时,以下 3 行代码可以加载 PDF,但不能在 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);
Run Code Online (Sandbox Code Playgroud)
WebLogic 12c 产生以下错误:
<[活动] ExecuteThread:队列为“6”:'weblogic.kernel.Default(自调整)'> <> <> <3902331f-a214-42fe-a6a1-35b3531e4b56-000000a9> <1523985710798> <[严重性值:8] [rid:0] [分区 ID : 0] [分区名称: DOMAIN] > <[ServletContext@1661988196[app:mmhsrp-ear-4.9.0.1-3 module:/mmhsrp path:null spec-version:3.1]] Servlet 失败并出现 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 (系统找不到指定的路径)
- 当我浏览文件系统时,要手动尝试使用 WebLogic 中的路径查找 pdf 文件,我最多可以找到此目录: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文件中确实是我的pdf文件:myPdfFile.pdf
-那么为什么类加载器无法找到/加载pdf文件呢?
- 它与 pdf 文件实际上位于存档的 _wl_cls_gen.jar 文件中并且未分解有什么关系吗?
- 关于我的 java 类如何在 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);
Run Code Online (Sandbox Code Playgroud)
假设资源可作为文件系统中的单个文件使用。正如您的错误消息和搜索显示的那样,WebLogic 中并非如此,而是包含在 jar 文件中。
因此,您的代码不应尝试通过File对象访问它。相反,InputStream可以通过以下方式进行访问,并且 PDFBox 也支持:
InputStream resource = classLoader.getResourceAsStream("myPdfFile.pdf");
PDDocument document = PDDocument.load(resource);
resource.close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2041 次 |
| 最近记录: |