2

我正在使用带有 JSF 的 Java 和 Glassfish 3 容器。在我的 Web 应用程序中,我正在尝试实现文件(图像)管理系统。

我有一个config.properties文件,我从中读取了应保存上传图像的路径。

save.file.path.event = D:\\upload

文件上传有效,所有文件都在原位,但是当我尝试列出图像时,浏览器说找不到图像。

<img src = "D:/upload/img1.png" />

Web 服务器位于完全不同的分区中。

除了没有找到图像的问题之外,我还有以下关于该领域的良好做法的问题:如果他们将是我将应用程序发布到 Web 服务器上的时间,那么使用类似的路径有多安全/正确D:\\upload?我想到的一些问题是操作系统之间的差异或分区名称的不确定性。

4

2 回答 2

2

您正在尝试使用本地路径引用图像。请记住,Web 浏览器(在客户端计算机上)正在解释该 img 标记,因此它将查看它自己的文件系统上的那个位置,而不是服务器的文件系统。您需要将链接指向您的服务器提供图像的网址。例如http://server/upload/img1.png.

于 2012-02-06T20:03:00.920 回答
1

It seems that there are two ways to solve this problem

1. Place the files in the Application

If the application name is MyApplication, you can specify a path like MyApplication/uploads/ images and the files will be saved inside the actual application. The downside of this method is that at every redeploy or hotdeploy the files will be deleted.

2. Place an absolute path in the web container config

For Tomcat go to: ../ Apache Tomcat x.x.xx/conf/server.xml and there write:

<Context path="/web_uploads" docBase="C:/uploads/"/>

For Glassfish go to: sun-web.xml file and write:

<sun-web-app>
    <property name="alternatedocroot_1" value="from=/uploads/* dir=C:"/>
</sun-web-app>
于 2012-02-08T19:00:33.373 回答