request.getSession().getServletContext().getRealPath("/upload/" + filename)
固然在運行的過程當中,咱們能夠設置 Tomcat 來達到上傳文件web
思路:apache
清空 /tomcat/webapps/
目錄項目文件緩存
修改 /tomcat/conf/web.xml
文件tomcat
<servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
修改 /tomcat/conf/server.xml
文件服務器
<Host name="localhost" appBase="webapps" uppackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <!-- 註釋如下代碼 --> <Valve vlassName="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/> <context docBase="D:\project\myProject\…\WebRoot" path="/"> </Host>
原理:app
D:\project\myProject\...\WebRoot
這個路徑是項目的 WEB 根目錄,設置之後上傳時就會在 upload 文件夾下上傳文件