我正在开发一个需要上传用户图像的简单 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 "%r" %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>
- 无需提及
localhost:9191
. - 确保您在 eclipse 之外运行此项目,因为 eclipse 会创建
context docBase
在其本地内自行输入server.xml
文件。
最佳答案
我自己解决了这个问题,考虑了以下几点:
- 无需提及
localhost:9191
和img src
- 确保您在 eclipse 之外运行此项目,因为 eclipse 在其本地
server.xml
文件中自行创建了context docBase
条目。
关于java - 无法在 apache tomcat 中使用 Context docBase 显示 html 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46947407/