[英]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
. 我正在开发一个jsf Web应用程序,其中已经在本地计算机上存储了一些图像文件,比如
D:\\images
。 The server running on this computer only. 服务器仅在此计算机上运行。 How can i access my local drive files on my web application.
如何在Web应用程序上访问本地驱动器文件。
I tried 我试过了
<p:graphicImage value="D:\\\\Temp\\tec0178.jpg">
or <p:graphicImage value="D:\\\\Temp\\tec0178.jpg">
或
`<p:graphicImage value="D:/Temp/tec0178.jpg">`
this not work for me. 这对我不起作用。 If i place the images in my web application
如果我将图像放在Web应用程序中
<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. 如果需要将其存储在服务器上部署应用程序的物理位置上,则可以使用File类在JAVA中使用基本的I / 0操作。 Local drives are directly accessible.
本地驱动器可直接访问。
http://docs.oracle.com/javase/tutorial/essential/io/ http://docs.oracle.com/javase/tutorial/essential/io/
You could also explore Google Guava API for the same. 您也可以探索Google Guava API。
A simple example using Google Guava API is: 使用Google Guava API的一个简单示例是:
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. 另外,要通过URL访问图像,您必须将目录添加为上下文。 Check out the blog post to access image file as URL:
查看博客文章以访问URL作为图像文件:
http://th1rty7.blogspot.in/2009/05/tomcat-is-often-considered-to-be-too.html 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? 这不完全是JSF的答案,但是我喜欢简单的想法:例如,为什么不编写一个称为Images的servlet,该servlet接收到本地目录的相对路由作为参数? That servlet would know exactly where to look (you could have d:\\Images stored in some properties) and would be as simple as:
该servlet会确切知道在哪里查找(您可以将d:\\ Images存储在某些属性中),并且非常简单:
<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. 最好将图像存储在Web应用程序本身中。 you can call it like
你可以这样称呼它
<img src="\images\appimage.jpg">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.