)1)准备前页Upload.html
表单操作=文件上传后台接口method=" post " enctype=" multipart/form-data "
文件输入框input type=”file "
formaction='/upload ' method=' post ' enctype=' multipart/form-data ' input type=' file ' name=' files ' input type
)3)准备接收文件的servlet
url路径必须与上面表单中的操作匹配
)4)编写文件上传后台代码
protectedvoiddopost (httpservletrequestrequest,http servlet响应) throws ServletException,io exception {/try } //解析请求的内容文件数据@suppresswarnings(unchecked ) ) listfileitemformitems=upload.parse request ); //迭代表单数据for(fileitemitem3360formitems )//表单中没有的字段if (! item.is form field ((string filename=item.getname ); //定义上载文件的存储路径string path=request.getservletcontext ().getrealpath )/uploadfiles ); //定义上载文件的完整路径string file path=string.format (' % s/% s ',path,fileName ); filestore file=new file (文件路径; //控制台输出文件上传路径system.out.println (文件路径); //将文件保存到硬盘item.write(storefile ) }}catch(exceptionex ) } )上的代码的路径,并提供用于存储上载文件的目录
注意:空目录idea不会更新到服务器,因此lmdxn可以随意包含和删除文件,以强制idea进行更新