Handle / access files on the disk
-
26-04-2021 - |
Question
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.
Solution 2
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>
OTHER TIPS
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
.