处理/访问磁盘上的文件
我将 Java 与 JSF 和 Glassfish 3 容器一起使用。 在我的网络应用程序中,我试图实现一个文件(图像)管理系统。
我有一个 config.properties 文件,我从其中读取上传图像的保存路径。
save.file.path.event = D:\\upload
文件上传正常,所有文件都在其位置,但当我尝试列出图像时,浏览器显示找不到图像。
<img src = "D:/upload/img1.png" />
Web 服务器位于完全不同的分区中。
除了找不到图像问题之外,我还有以下关于该领域良好实践的问题:如果我将在网络服务器上发布应用程序,那么使用路径的安全性/正确性如何像D:\\upload
?我想到的一些问题是操作系统之间的差异或分区名称的不确定性。
I'm using Java with JSF an Glassfish 3 container.
In my web application I'm trying to implement a file(image) management system.
I have a config.properties
file from where I read the path where the uploaded images should be saved.
save.file.path.event = D:\\upload
The file upload works and all the files are in their place but when I try to list the image the browser says that no image could be found.
<img src = "D:/upload/img1.png" />
The web server is in a totally different partition.
Besides the no image found problem I have the following question regarding good practices in this area: If their will be a time when I will publish the application on a web server how safe / correct is to use a path like D:\\upload
? Some issues which come to my mind are the differences between operating systems or the uncertainty of a partition name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试使用本地路径引用图像。请记住,Web 浏览器(在客户端计算机上)正在解释该 img 标签,因此它将查看其自己的文件系统而不是服务器的文件系统上的该位置。您需要使链接指向您的服务器提供图像的网址。例如
http://server/upload/img1.png
。You are trying to reference the image with a local path. Keep in mind that the web browser (on the client machine) is interpreting that img tag, so it's going to look at that location on it's own file system rather than the server's file system. You need to make the link point to the web address that your server is serving the image up at. For example
http://server/upload/img1.png
.看来有两种方法可以解决这个问题
1。将文件放在应用程序中
如果应用程序名称是
MyApplication
,您可以指定MyApplication/uploads/images
这样的路径,文件将保存在MyApplication
中实际应用。此方法的缺点是,每次重新部署或热部署时,文件都会被删除。2.在 Web 容器配置中放置绝对路径
对于 Tomcat,请转到:
../ Apache Tomcat xxxx/conf/server.xml
并在那里写入:对于 < em>Glassfish 转到:
sun-web.xml
文件并写入: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 likeMyApplication/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:For Glassfish go to:
sun-web.xml
file and write: