java - 无法在 apache tomcat 中使用 Context docBase 显示 html 图像

标签 java html image apache tomcat

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

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

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

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

但是我无法在服务器启动时显示图像。下面是我的短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 会创建 context docBase在其本地内自行输入 server.xml文件。

最佳答案

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

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

关于java - 无法在 apache tomcat 中使用 Context docBase 显示 html 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46947407/

相关文章:

java - < 和 > 是什么意思,比如 implements Comparable<BigInteger>?

java - 解释代码(带有数组的 if 语句)

html - 为什么字体大小不一样

python - 从图像中删除颜色

android - 我可以在android模拟器中使用alsa运行应用程序吗?

javascript - 如果图像已经开始加载,显示 div

java - 将 JComboBox 添加到 JXTable 单元格时遇到问题

java - 在 IntelliJ 中应用格式化编码风格

html - CSS3选择带有嵌套表的顶级表中的所有td

javascript - 在 html 中下拉登录