项目中经常使用文件上传,Chrome出于安全考虑不能直接读取硬盘上的资源,此时就需要我们用到虚拟路径
我使用的idea+Tomcat
1.直接更改Tomcat的server.xml
a.Tomcat中的配置: 添加红色字体
<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" />
-->
<!-- 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" />
<!-- 设置图片虚拟路径[访问时路径为/photo] -->
<Context path="/photo" docBase="D:\delelop\temp" reloadable="true" />
b.Idea中的配置,勾上Deploy applications configured in Tomcat instance
c.View层代码
<tr>
<td>商品图片</td>
<td>
<c:if test="${itemsCustom.pic != null}">
<img src="/photo/${itemsCustom.pic}" width=100 height=100/><br/>
</c:if>
<input type="file" name="items_pic"/>
</td>
</tr>
2.不更改Tomcat的配置文件,直接在idea中配置虚拟文件路径
a.不勾上Deploy applications configured in Tomcat instance
b.打开项目Setting设置,(只能对单个文件夹生效)