1:文件上传时,上传到webapps目录下的文件会自动删除的缘由?html
tomcat目录下的webapps 文件夹是部署目录,当项目从新部署(Tomcat重启、代码改变时的自动部署),上传的文件不在部署文件的范畴内,即此时开发工具中没有上传的文件,因此上传的文件就没有了。java
清理缓存也会使上传到webapps目录下的文件文件消失。web
2:解决方法apache
把文件上传路径设置到webapps目录以外,能够在Tomcat目录下创建一个文件夹,如:upload缓存
3:简单实现tomcat
html文件:app
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="../jsp/smartupload.jsp" method="post" enctype="multipart/form-data"><input type="file" name="file"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
jsp文件:eclipse
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% SmartUpload smart=new SmartUpload(); smart.initialize(pageContext); smart.upload(); //将文件上传到项目目录下,从新部署后会自动消失 //smart.save(request.getSession().getServletContext().getRealPath("/")+"/upload"); //经过绝对路径将文件上传到Tomcat目录下的upload文件夹内,则从新部署后不会消失 smart.save("D:\\software\\learningsoftware\\tomcat\\apache-tomcat-7.0.72\\upload"); %>
</body>
</html>
此外,直接在Tomcat/webappps目录下的项目文件中添加文件、在lib中放入jar包、修改web.xml,配置server.xml等,当Tomcat重启(或代码改变时自动部署),eclipse就会从新部署项目,或者清理缓存后,那么直接在webapps下的项目目录中进行的操做就失效了。webapp
解决方法:jsp
一:直接在eclipse中进行操做
如:直接在eclipse中的web项目下添加文件,导入jar包(直接复制粘贴,再build path-add to build path),修改web.xml文件,在servers项目中修改server.xml等,即使从新部署这些操做也不会消失,由于这些内容已经存在于eclipse中了。
二:取消eclipse的自动发布功能
Window->Preferences->Server->Launching->点击取消Automatically publish wen starting servers