Java:无论应用服务器如何,通用访问资源文件 [英] Java: Universally accessing resource files regardless of application server

查看:24
本文介绍了Java:无论应用服务器如何,通用访问资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找访问资源文件(例如一堆 .xml 文件)以及它们所在的文件夹结构的最佳实践"方式,无论使用何种应用服务器.

I'm looking for the 'best practice' way to access resource files (for example a bunch of .xml files) as well as the folder structure in which they reside, regardless of the application server used.

现在我正在使用 Wildfly 8 服务器,我通过获取应用程序真实路径然后使用 Paths.get(resourcePath)src/main/resources/xxxx> 以及 Files.walk(Paths.get(folderPath)) 如果我想访问文件夹的文件.

Right now I'm using Wildfly 8 server and I access all src/main/resources/xxxx by getting the application real path then using Paths.get(resourcePath) as well as Files.walk(Paths.get(folderPath)) if I want to access a folder's files.

然而,当我尝试部署到 Weblogic 12c 时遇到了一个问题,因为这个应用服务器实际上获取了 WEB-INF/classes 下的所有内容并创建了一个 .jar文件并将其添加到 WEB-INF/lib.我仍然可以使用 classLoader.getResource(resourcePath) 访问单一资源,但是由于某种原因,当我尝试创建一个 new File(Paths.get(resourcePath) 或使用 >Files.walk(Paths.get(folderPath)) 它似乎不起作用.它抛出一个异常,说文件不存在,我猜这是因为它不可访问,因为它是打包在 jar 文件中.

However, I faced a problem when I tried to deploy to Weblogic 12c, because this app server actually takes everything under WEB-INF/classes and creates a .jar file and adds it to WEB-INF/lib. I can still access singular resources using classLoader.getResource(resourcePath) but for some reason when I try to create a new File(Paths.get(resourcePath) or use Files.walk(Paths.get(folderPath)) it doesn't seem to be working. It throws an exception saying to file doesn't exist which I'm guessing is because it is not accessible since it is packaged inside a jar file.

我可能会使用 classLoader.getResource(resourcePath) 来访问我的所有资源,但不幸的是,在我的情况下,我不知道在编译时哪些资源可用.我特别需要能够浏览选定文件夹的文件和子文件夹,但我还没有找到在两个应用服务器或所有应用服务器上执行此操作的通用方法.

I could potentially use classLoader.getResource(resourcePath) to access all my resources but unfortunately in my case I cannot know what resources will be available at compile time. I specifically NEED to be able to go through a selected folder's files and subfolders but I haven't found a common way to do it on both app servers, or ALL app servers for that matter.

如果解决方案使用新的 File api 而不是创建一堆 FileReaders,我会加分,但我也同意.

Bonus points if the solution uses the new File api instead of creating a bunch of FileReaders but I'm ok with that too.

推荐答案

您可以将 XML 文件放在文件夹 /WEB-INF/xml 中,然后使用 ServletContext> 获取该位置的 FilePath.

You could place the XML files in a folder /WEB-INF/xml and then use the ServletContext to obtain a File or Path for that location.

变体 1:

调用 servletContext.getResourceUrl("/WEB-INF/xml") 获取 URL 并将此 URL 转换为 File路径.但是根据服务器,这可能会返回一个非文件资源 URL,例如一个 jndi:/ 网址.

call servletContext.getResourceUrl("/WEB-INF/xml") to obtain a URL and convert this URL to a File or Path. But depending on the server this might return a non-file resource URL, e.g. a jndi:/ url.

变体 2:

调用 servletContext.getRealPath("/WEB-INF/xml") 获取文件字符串并将此 URL 转换为 FilePath.

call servletContext.getRealPath("/WEB-INF/xml") to obtain a file string and convert this URL to a File or Path.

这篇关于Java:无论应用服务器如何,通用访问资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆