需要commons-fileupload-1.2.1.jar包 jsp <form name="frm" action="<%=basePath %>admin/ExchArticle?method=save" method="post" enctype="multipart/form-data"> <table> <tr><td>文章标题</td><td><input type="text" name=""></td></tr> <tr><td>文章分类</td><td><select name=""><option></option></select></td></tr> <tr><td>文章重要性</td><td><input type="radio" name="" value="0">普通<input type="radio" name="" value="1">置顶</td></tr> <tr><td>是否显示</td><td><input type="radio" name="" value="0">显示<input type="radio" value="1">不显示</td></tr> <tr><td>文章作者</td><td><input type="text" name=""></td></tr> <tr><td>作者email</td><td><input type="text" name=""></td></tr> <tr><td>关键字</td><td><input type="text" name=""></td></tr> <tr><td>外部链接</td><td><input type="text" name=""></td></tr> <tr><td>上传文件</td><td><input type="file" name="filepath"></td></tr> <tr><td >文章内容</td><td width="600"><textarea name="DESCRIPTION" cols="800" rows="80"> servlet private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.debug("save ExchArticleServlet instance"); String filerealpath=""; //String webrealpath=this.getServletContext().getRealPath("")+"//admin//web//file//"; String webrealpath=this.getServletContext().getRealPath("")+"/admin/web/file/"; FileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(factory); System.out.println("webrealpath======"+webrealpath); try { List<FileItem> items=upload.parseRequest(request); for(FileItem fi:items){ if(!fi.isFormField()){ String a=getName(fi.getName()); filerealpath=webrealpath+getFileName(fi.getName()); File uploadedFile=new File(filerealpath); fi.write(uploadedFile); } } } catch (Exception e) { e.printStackTrace(); } } private String getFileName(String fileName){ long time=System.currentTimeMillis(); if(fileName.lastIndexOf("//")>0){ return time+fileName.substring(fileName.lastIndexOf("//")+1); }else{ return time+fileName; } } private String getName(String fileName){ if(fileName.lastIndexOf("//")>0){ return fileName.substring(fileName.lastIndexOf("//")+1,fileName.indexOf(".")); }else{ return fileName.substring(0,fileName.indexOf(".")); } }