java 在 JSP 中显示带有绝对路径的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10509192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:21:38  来源:igfitidea点击:

Showing image with absolute path in JSP

javaimagejsppathjetty

提问by Vitor Braga

I'm using Java, JSP and Jetty to develop a website.

我正在使用 Java、JSP 和 Jetty 来开发网站。

I'm having some problem with images paths passed in src of <img>tags.

我在<img>标签的src 中传递的图像路径有一些问题。

In my database, I have the Linux absolute path of the users' images. I get this value and put into the "src" attribute.

在我的数据库中,我有用户图像的 Linux 绝对路径。我得到这个值并放入“src”属性中。

I have the path, for example:

我有路径,例如:

  • /home/website/images/image1.png
  • /home/website/images/image1.png

Making this way, the image doesn't appear. Thinking for a long time, I've concluded that's natural because I've never seen in any website image with such path to an absolute path of the OS.

这样做,图像不会出现。思考了很长时间,我得出的结论是,这是很自然的,因为我从未在任何网站图像中看到过这种路径指向操作系统的绝对路径。

What I need to know is what I have to do?

我需要知道的是我必须做什么?

Do I have to alter Jetty's configuration? Do I have to run some code in Java?

我必须改变 Jetty 的配置吗?我必须在 Java 中运行一些代码吗?

Searching through Google, I got more confused. I would like an explanation.

通过谷歌搜索,我变得更加困惑。我想要一个解释。


UPDATE (26/03/2013)


更新 (26/03/2013)

I found this way of treating this issue:

我找到了这种处理这个问题的方法:

http://balusc.blogspot.com.br/2007/04/imageservlet.html

http://balusc.blogspot.com.br/2007/04/imageservlet.html

Thank you BalusC! hauhauhahaua

谢谢 BalusC!哈哈哈哈哈

回答by BalusC

Indeed, the <img src>must refer a public web URL, not a local disk file system path. Your images have to be available by a HTTP request. It's namely the webbrowser who has got to download them, not the webserver who has got to include them somehow.

实际上,<img src>必须引用公共 Web URL,而不是本地磁盘文件系统路径。您的图像必须通过 HTTP 请求可用。即必须下载它们的网络浏览器,而不是必须以某种方式包含它们的网络服务器。

The most straightforward way for starters would be to create a servlet which gets the image content as an InputStreamby FileInputStreambased on the request parameter or path info and then writes it the usual Java IO way to OutputStreamof the HttpServletResponse, after having set the necessary HTTP response headers so that the browser understands how to deal with it.

对于初学者的最直接方式是创建一个servlet它获取的图像内容作为一个InputStream通过FileInputStream基于该请求参数或路径信息,然后将其写入通常的Java IO方式OutputStreamHttpServletResponse,具有设置必要的HTTP响应报头,以便后浏览器了解如何处理它。

E.g., assuming that the servlet is mapped on /images/*and you open the image as http://example.com/contextpath/images/image1.png:

例如,假设 servlet 已映射,/images/*并且您将图像打开为http://example.com/contextpath/images/image1.png

String filename = request.getPathInfo();
File file = new File("/home/website/images", filename);

response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));

InputStream input = new FileInputStream(file);
OutputStream output = response.getOutputStream();
// Stream bytes the usual way.

An alternative is to add /home/website/imagesas a new web application context to the server with the context path of /images. This way the image would be available by http://example.com/images/image1.pngYou're only dependent on the servletcontainer make/version how to configure that and also whether you've full admin control over it. For Jetty, that would be the following if you're managing it programmatically:

另一种方法是将/home/website/images上下文路径为/images. 这样,图像可以通过http://example.com/images/image1.png获得。您只依赖于 servletcontainer make/version 如何配置它,以及您是否对它具有完全的管理员控制权。对于 Jetty,如果您以编程方式管理它,则如下所示:

server.setHandler(new WebAppContext("/home/webapp/images", "/images"));

Or when you're managing it by XML:

或者当您通过 XML 管理它时:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="resourceBase">/home/website/images</Set>
    <Set name="contextPath">/images</Set>
</Configure>

回答by jmort253

Judging by the words you used in your URI, it looks like you are putting the path to the file on the filesystem in the src attribute instead of the URL that uniquely identifies the resource.

根据您在 URI 中使用的词语判断,您似乎将文件系统上的文件路径放在 src 属性中,而不是唯一标识资源的 URL。

Absolute paths are not absolute from your filesystem, they are absolute from the root of your Web server. Additionally, those URL resource identifiers can even be changed or modified to map to different folders in your server or even dynamic resources.

绝对路径不是来自文件系统的绝对路径,而是来自 Web 服务器根目录的绝对路径。此外,这些 URL 资源标识符甚至可以更改或修改以映射到服务器中的不同文件夹甚至动态资源。

Here is an example:

下面是一个例子:

Path to file on server:

服务器上的文件路径:

/home/website/images/image1.png

example.com is mapped to "website" folder:

example.com 映射到“网站”文件夹:

When using a relative URL, perhaps you're seeing:

使用相对 URL 时,您可能会看到:

<img src="/images/image1.png" />

To use the absolute URL, you would need the HTTP scheme:

要使用绝对 URL,您需要 HTTP 方案:

<img src="http://example.com/images/image1.png" />

In summary, using file paths to identify resources on your server from the client side HTML is just incorrect and will not work.

总之,使用文件路径从客户端 HTML 识别服务器上的资源是不正确的,并且不起作用。