上传文件
相对路径的 写法
1. File uploadPath =new File("D:\\addnetFile\\tmp\\") 绝对路径
2. req.getSession().getServletContext().getRealPath("images/item/")
获得相对路径,会随webapp位置的改变而改变,但一定在webapp的位置之下,不会在它之外(缺点)
3. 由servlet初始化参数获得,也是绝对路径,但可通过改变xml文件改变
public void init() throws ServletException {
this.uploadPath = this.getServletConfig().getInitParameter("upload-path");
}
<servlet>
<servlet-name>FileUpload</servlet-name>
<servlet-class>com.bjsxt.drp.util.servlet.FileUpload</servlet-class>
<init-param>
<param-name>upload-path</param-name>
<param-value>C:\\apache-tomcat-5.5.17\\webapps\\drp1.9\\images\\item\\</param-value>
</init-param>
</servlet>