1.jsp文件
1.在jsp添加表单,设置enctype=“multipart/form-data”
<form action="page.do" class="" method="post" enctype="multipart/form-data">
<input type="text" class="" name="blogtitle" placeholder="输入标题" /><br />
<input type="text" class="content" name="blogcontent"
placeholder="输入正文" /><br> <input type="hidden" name="op"
value="add" />
<input type="file" name="myfile" >
<button type="submit" class="btn btn-primary">发布</button>
</form>
2.Servlet
1.在Servlet中加注解@MultipartConfig
2.获取文件:Part part = reuqest.getPart(“myfile”);
3.获取存放目标位置:String path = request.SetServletContext.getReallpath("/picture/");
4.获取文件名:String filename = part.getSubmittedFilename();
5.将上传文件写进目标文件位置
part.wtite.(path+part.getSubmittedFilename());
//获取上传文件
Part part = request.getPart("myfile");
//获取实际picture目录路径
String path=request.getServletContext().getRealPath("/picture/");
System.out.println(path);
//获取文件名
String filename = part.getSubmittedFileName();
System.out.println(filename);
blog.setFilename(filename);
//将上传文件写进目标位置(Tomcat目录Webapps/当前工程目录下)
part.write(path+part.getSubmittedFileName());
System.out.println("写进成功");
上传的文件存在Tomcat服务器下的webapps/project/picture