简体   繁体   English

如何在Web应用程序中获取服务器本地驱动器路径?

[英]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.

相关问题 如何从Linux服务器Java Web应用程序访问本地路径? - How to access local path from linux server java web application? 使用本地Web服务器使用Java应用程序对Google驱动器进行身份验证 - Authenticating google drive with a java application using a local web server 如何使用Java在文件上传中获取本地磁盘文件路径? 如果是基于表单的Web应用程序...? - How to get local disk file path in file upload using java? If it is form based web application…? 如何在Java应用程序中获取映射的网络驱动器的UNC路径 - How to get the UNC path of a mapped network drive in a Java application 如何在 Web 应用程序中获取当前目录的路径 - How to get path of current directory in Web Application 如何获取运行时Web应用程序路径? - How to get Runtime Web Application path? 如何获取Java Web应用程序路径? - How to get Java web application path? 如何将 Web 套接字从 Internet Web 应用程序发送到本地服务器 - How to send web sockets from an internet web application to a local server 如何在应用程序服务器中获取.ear文件的路径? - How to get the path of an .ear file in application server? 本地驱动器中图像的路径 - Path to an image in the local drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM