【发布时间】: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 "%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 会在其本地
server.xml文件中自行创建context docBase条目。
【问题讨论】:
标签: java html image apache tomcat