java - 如何为 GlassFish Web 应用程序提供对外部资源(文件)的访问?

标签 java file glassfish resources web-applications

我有点GlassFish初学者,所以请原谅我对这个问题的无知。

基本上我们正在为一个游戏网站提供服务,为了让我们的网络应用程序可以下载客户端,我们将其复制到 domain1 中的一个目录中。这样做的问题是,当重新部署 Web 应用程序时,可下载的客户端丢失了,我们必须再次复制它。

我希望能够将可下载的客户端存储在某个外部位置,并让 GlassFish 提供对它的访问。

我可以将链接硬编码到 Web 应用程序中,但这样我们就会失去可移植性,因此这就是让 GlassFish 处理它的原因。

我也可以将客户端下载到我们的数据库中,但这似乎是对数据库的不当使用,也可能导致数据库性能不佳。

我找到的第三个选项是添加从某个名称到文件位置的自定义资源映射,然后在我的一个 bean 中提供一种方法来检索文件位置。这似乎只是为了拥有一个外部资源而做的很多工作,我觉得一定有更简单的方法。

那我该怎么办呢?

最佳答案

使用 GlassFish,您可以定义备用文档根目录来为来自 war 之外的文件提供服务。来自文档:

Alternate Document Roots

An alternate document root (docroot) allows a web application to serve requests for certain resources from outside its own docroot, based on whether those requests match one (or more) of the URI patterns of the web application's alternate docroots.

To specify an alternate docroot for a web application or a virtual server, use the alternatedocroot_n property, where n is a positive integer that allows specification of more than one. This property can be a subelement of a sun-web-app element in the sun-web.xml file or a virtual server property. For more information about these elements, see sun-web-app in Oracle GlassFish Server 3.0.1 Application Deployment Guide.

所以你可以这样配置:

<property name="alternatedocroot_1" value="from=/ext/* dir=/path/to/ext"/>

有关完整详细信息,请参阅文档。

关于java - 如何为 GlassFish Web 应用程序提供对外部资源(文件)的访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3266608/

相关文章:

java - 使用 DatagramSockets 的迭代 DNS 客户端

Java 安全注释被忽略

java - Glassfish 5 可以与 java 10 一起运行还是仅与 java 8 兼容?

testing - 无法访问远程 EJB

java - 在 Maven 中,如果插件在未激活的激活配置文件中声明,则扩展打包类型会抛出 "unknown packaging error"

java - 从特定模式拆分字符串

Android 创建一个可以附加到电子邮件的内存驻留输入文件

java - 为什么要将 && 更改为 ||导致变量不可见

c - C语言读写大文件的有效方法

java - 无法在 AWS 实例 (Linux) 上编译 GWT 应用程序