public class SmartUploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = getServletContext().getRealPath("/")+"images"; File file = new File(filePath); if(!file.exists()){ file.mkdir(); } SmartUpload su = new SmartUpload(); //初始化; su.initialize(getServletConfig(), request, response); //设置上传文件大小 su.setMaxFileSize(1024*1024*10); //设置所有的上传文件的大小 su.setTotalMaxFileSize(1024*1024*100); //设置允许上传文件类型 su.setAllowedFilesList("txt,jpg,gif"); String result = "上传成功"; try { su.upload(); su.save(filePath); } catch (Exception e) { result = "上传失败"; if(e.getMessage().indexOf("1015")!=-1){ result="上传失败:上传文件类项不正确"; }else if(e.getMessage().indexOf("1010")!=-1){ result="上传失败:上传文件类项不正确"; } else if(e.getMessage().indexOf("1105")!=-1){ result="上传失败:上传文件大小大于允许上传的最大值"; } else if(e.getMessage().indexOf("1110")!=-1){ result="上传失败:上传的总的文件大小大于允许上传的最大值"; } } request.setAttribute("result", result); request.getRequestDispatcher("jsp/02.jsp").forward(request, response); } }