我需要查看我上传的图片的正确路径是什么? [英] What is the correct path I need to see my uploaded image?

查看:120
本文介绍了我需要查看我上传的图片的正确路径是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的web应用程序中,我的一个页面正在将照片上载到路径中。


$ b

 / usr / local / rac / picture-name-goes-here 

照片上传正常,但我需要在另一个页面中访问它,当我尝试从我的JSP访问它时,它不会显示,我猜我的照片路径不正确。



我的JSP中用于访问照片的代码如下所示。

 < tr> 
< td>
< img src =/ usr / local / agent / photo-name-here.jpg/>
< / td>
< / tr>

我的照片路径不正确吗?
如果有帮助,我从Tomcat运行我的Web应用程序,该目录位于目录



C:\ Tomcat6



我最终会将其转移到Linux机器上,并期望与照片共享相同的路径。 解决方案

这里有一个主要的误解。 HTML由网页浏览器执行,而不是由网络服务器执行。 webbrowser下载HTML,扫描任何需要下载的资源(CSS,脚本,图像等),并为每个资源启动一个新的HTTP请求。所有资源都应该指向有效的URL ,而不是指向客户端计算机不具备的某个本地磁盘文件系统路径。



基本上有两种方法可以解决这个问题:


  1. 添加一个新的上下文到Tomcat的 /conf/server.xml

     <上下文docBase =/ usr / local / agentpath =/ images/> 

    这样他们可以通过 http://example.com/images/ ...你可以使用下面的< img> $ b

     < img src =/ images / photo-name-here.jpg/> 


  2. 创建一个 Servlet 获取图像的 InputStream ,并将其写入响应的 OutputStream 中。您可以在这里找到 这样一个servlet的基本示例,这里是一个更高级的例子。当 Servlet 映射到 web.xml / images / * 时$ c>,图片可通过 http://example.com/contextname/images/ 访问...您可以按如下方式使用它(假设JSP / HTML文件位于上下文根目录中):

     < img src =images / photo-name-here.jpg/> 



In my web application one of my pages is uploading a photo to the path

/usr/local/rac/picture-name-goes-here

The photo is uploading fine, but I need to access it in another page and when I try to access it from my JSP, it will not show up, I am guessing my path to the photo is incorrect

The code in my JSP to access the photo looks like the following.

            <tr>
                <td>
                    <img src="/usr/local/agent/photo-name-here.jpg"/>
                </td>
            </tr>

Am I incorrect with this path to the photo? If it helps, I am running my web application from Tomcat which is in the directory

C:\Tomcat6

I will eventually be moving this over to a linux machine and expect to share the same path to the photo.

解决方案

There is one major misconception here. HTML is executed by the webbrowser, not by the webserver. The webbrowser downloads HTML, scans for any resources which needs to be downloaded as well (CSS, scripts, images, etc) and fires a new HTTP request for each of them. All resources should point to a valid URL, not to some local disk file system path which the client machine has no notion of.

There are basically two ways to solve this "problem":

  1. Add a new Context to Tomcat's /conf/server.xml:

    <Context docBase="/usr/local/agent" path="/images" />
    

    This way they'll be accessible through http://example.com/images/... and you'll be able to use the following <img>

    <img src="/images/photo-name-here.jpg"/>
    

  2. Create a Servlet which basically gets an InputStream of the image and writes it to the OutputStream of the response along a correct set of headers. You can find here a basic example of such a servlet and here a more advanced example. When the Servlet is mapped on /images/* in web.xml, the images are accessible by http://example.com/contextname/images/... and you'll be able to use it as follows (assuming that the JSP/HTML file is located in the context root):

    <img src="images/photo-name-here.jpg"/>
    

这篇关于我需要查看我上传的图片的正确路径是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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