简体   繁体   中英

How to get server local drive path in web application?

I am developing a jsf web application in which i have store some image files on my local computer lets say D:\\images . The server running on this computer only. How can i access my local drive files on my web application.

I tried

<p:graphicImage value="D:\\\\Temp\\tec0178.jpg"> or

`<p:graphicImage  value="D:/Temp/tec0178.jpg">` 

this not work for me. If i place the images in my web application

<p:graphicImage  value="resources/images/Male.png"/>

its working.

If you need to store it on a physical location on your server where your application is deployed, you could just work with basic I/0 operations in JAVA using File class. Local drives are directly accessible.

http://docs.oracle.com/javase/tutorial/essential/io/

You could also explore Google Guava API for the same.

A simple example using Google Guava API is:

File imageFile = new File("D:\\images", imgFileName);
FileOutputStream outputStream = new FileOutputStream(newAttachment);
        ByteStreams.copy(inputStream, outputStream);

Also, to access images over URL you'll have to add the directory as context. Check out the blog post to access image file as URL:

http://th1rty7.blogspot.in/2009/05/tomcat-is-often-considered-to-be-too.html

This is not exactly a JSF answer, but I like easy ideas: why don't you write a servlet called Images, for example, that receives as a parameter relative routes to your local directory? That servlet would know exactly where to look (you could have d:\\Images stored in some properties) and would be as simple as:

<img src="http://yourserver/yourapp/Images?route=someImage.jpg"></img>

would lead to

ImagesServlet.java

...
// assume 'properties' is some way to access your application properties, be it in database,
// or .properties file, or whatever suits you best.  In this case, it points to d:\Images
File imageFile = new File(properties.getRoute(), request.getParameter("route"));
InputStream is = new FileInputStream(imageFile);
ByteStreams.copy(inputStream, request.getOutputStream());

It is better to store your images in web application itself. you can call it like

<img src="\images\appimage.jpg">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM