如何从文件系统调用图像? [英] How to call images from file system?

查看:118
本文介绍了如何从文件系统调用图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用eclipse WTP在部署在tomcat服务器上的Ubuntu OS上开发Web应用程序。我想在Web应用程序中使用我的文件系统中的图像(显示它们)。我怎样才能有效地做到这一点?是通过使用上下文路径到驱动器上的位置?或者是通过使用流媒体加载它们(或类似的东西)?另外,我在WTP项目中找不到任何web.xml或server.xml文件(因为新版本甚至不需要它们)。

I am using eclipse WTP for developing web applications on on Ubuntu OS being deployed on a tomcat server. I want to make use of images in my file system in the web app (display them). How can I do it efficiently ? Is it by using context path to location on drive ? or is it by loading them (or something like that) using streaming ? Also, I could not find any web.xml or server.xml file in WTP project (since newer version doesn't even require them).

改述:我想要在我的网络应用程序中使用文件系统中的图像(静态内容)。在前端使用JSTL。

Rephrasing : I want to use images (static content) from file system in my web app. Using JSTL on the front-end.

如果网络应用 xyz 然后它的位置是: / home / webaapp / xyz /.....,图片位于 / home / akshay / images /.......

If web app is xyz then its location is : /home/webaapp/xyz/..... and images are at /home/akshay/images/.......

我想从网络远程访问一个文件夹(在同一硬盘中) app

I want to access a folder far away (in same hard drive) from web app

推荐答案

你可以使用 Tomcat默认Servlet 用于提供静态资源。

You can use Tomcat Default Servlet to serve static resources.

web.xml:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

更好地使用 c:url 并使用前缀值中的 / 使url相对于上下文路径。

Better use c:url and use prefix / in the value to make the url relative the context path.

<img id="logo" src="<c:url value='/resources/images/logo.png'/>" />

动态项目结构:

WebContent
         |
         |__resources
         |          |
         |          |__images
         |                   |
         |                   |__logo.png
         |
         |__WEB-INF
                  |
                  |__web.xml






编辑



由于图像不是战争的一部分,因此您可以尝试使用Servlet。只需将路径存储在属性文件中的某个位置,或将其作为VM参数传递或使其保持不变。


EDIT

Since the images are not part of the war hence you can try with Servlet. Just store the path somewhere in properties file or pass it as VM arguments or make it constant.

JSP:

<img src="${pageContext.servletContext.contextPath}/servletURL?name=logo.png"/>

Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String imageName=request.getParameter("name")
    String path = "absolute path of the image directory"+imageName;
    BufferedInputStream inputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(new File(path)));
        OutputStream outputStream = response.getOutputStream();
        byte[] bytes = new byte[1024 * 2];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, bytesRead);
        }
        outputStream.flush();
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

使用 try-with-resources语句,用于处理资源,如果您使用的是Java 7 。

Use The try-with-resources Statement to handle the resources, if you are using Java 7.

这篇关于如何从文件系统调用图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆