【问题标题】:not able to display html image using Context docBase in apache tomcat无法在 apache tomcat 中使用 Context docBase 显示 html 图像
【发布时间】:2018-04-07 10:16:24
【问题描述】:

我正在开发一个需要上传用户图像的简单 Java Web 应用程序。

首先,我使用了getServletContext().getRealPath()。但是后来有人建议,这不是一个好方法,因为重新部署应用程序时图像会丢失。我认为它是正确的。

然后我将这些图像上传到外部根应用程序文件夹,即服务器上“D:\images”路径上的某个位置。

然后为了显示这些图像,我阅读了this 线程,该线程描述了如何通过在tomcat 的conf 目录下的server.xml 下设置<host> 标记下的<Context docBase="D:\images" path="/myprojimages" /> 来显示图像。

但我无法在服务器启动时显示图像。以下是我的简短HTML 文件。

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    hello<br><br>
    <img src="<img src='http://localhost:9191/myprojimages/Facebook_icon.jpg" width="30" height="30" />
</body>
</html>

下面是 server.xml 文件中的条目

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <Context docBase="D:\dumpData"  path="/myprojimages" />
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

任何人都可以建议或纠正我哪里错了..因为图像没有显示?

更新

<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    hello<br><br>
    <img src="<img src='/myprojimages/Facebook_icon.jpg" width="30" height="30" />
</body>
</html>
  1. 不用提localhost:9191
  2. 确保您在 Eclipse 之外运行此项目,因为 Eclipse 会在其本地 server.xml 文件中自行创建 context docBase 条目。

【问题讨论】:

    标签: java html image apache tomcat


    【解决方案1】:

    我能够自己解决这个问题,考虑到以下几点:

    1. 无需提及localhost:9191img src
    2. 确保您在 Eclipse 之外运行此项目,因为 Eclipse 会在其本地 server.xml 文件中自行创建 context docBase 条目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-13
      • 2013-03-22
      • 2020-03-19
      • 2013-05-05
      • 1970-01-01
      • 2020-07-03
      相关资源
      最近更新 更多