日期:2014-05-20 浏览次数:20898 次
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String expertName =""; String sex = ""; String qq = ""; String fangxiang = ""; String expertphoto=""; String expertintr=""; String pwd1 = ""; String pwd2 = ""; UserDao userDao = new UserDao(); Expert expert = new Expert(); Map<String,String> addExpertError = new HashMap<String,String>(); //获取上传文件工厂 DiskFileItemFactory factory=new DiskFileItemFactory(); //设置上传工厂的限制 factory.setSizeThreshold(1024*1024*20); //设置上传的路径 System.out.println(request.getContextPath()); factory.setRepository(new File(request.getContextPath()+"/images/")); //根据上传工厂获取servletfileupload对象 System.out.println(new File(request.getContextPath()+"/images/")); ServletFileUpload upload=new ServletFileUpload(factory); //设置上传文件的大小 upload.setSizeMax(1024*1024*20); List items=null; try { items=upload.parseRequest(request); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } for(Iterator it=items.iterator();it.hasNext();){ FileItem item=(FileItem)it.next(); if(item.isFormField()){ //根据不同的属性加进 String name=item.getFieldName(); // System.out.println(name); // System.out.println("------------------"+request.getRealPath("/")); if(name.equals("name")){ expertName = item.getString("gbk"); if(null==expertName||"".equals(expertName)){ addExpertError.put("nullname", "用户名不能为空!"); }else{ if(userDao.isExist(expertName)){ addExpertError.put("nameexist", "用户名已经存在!"); }else{ expert.setExpertName(expertName); } } } if(name.equals("sex")){ sex = item.getString("gbk"); expert.setExpertSex(sex.equals("man")?true:false); } if(name.equals("pwd1")){ pwd1 = item.getString("gbk"); if(null==pwd1||"".equals(pwd1)){ addExpertError.put("pwderror", "密码不能为空!"); } } if(name.equals("pwd2")){ pwd2 = item.getString("gbk"); if(!pwd1.equals("")&&!pwd2.equals(pwd1)){ addExpertError.put("pwdnotequal", "密码不一致!"); } if(!pwd1.equals("")&&pwd2.equals(pwd1)){ expert.setExpertPwd(pwd2); } } if(name.equals("qq")){ qq = item.getString("gbk"); if(null==qq||"".equals(qq)){ addExpertError.put("nullqq", "用户QQ不能为空!"); }else{ expert.setExpertQQ(qq); } } if(name.equals("fangxiang")){ fangxiang = item.getString("gbk"); if(null==fangxiang||"".equals(fangxiang)){ addExpertError.put("nullfangxiang", "咨询方向不能为空!"); }else{ expert.setExpertSpeciality(fangxiang); } } if(name.equals("content")){ expertintr = item.getString("gbk"); if(null==expertintr||"".equals(expertintr)){ addExpertError.put("nullintro", "专家介绍不能为空!"); }else{ expert.setExpertIntro(expertintr); } } }else{ if(addExpertError.size()!=0){ request.setAttribute("addExpertError", addExpertError); request.getRequestDispatcher("/error.jsp").forward(request,response); } //获取字段的名字 String fieldName=item.getFieldName(); //获取文件的名字 String fileName=item.getName(); //获取文件的名字可以根据不同的后缀进行处理 System.out.println(fileName); // int size = fileName.lastIndexOf("/"); // fileName = expertphoto+fileName.substring(size,fileName.length()); // System.out.println(fileName); //取得文件的类型 String contentType=item.getContentType(); System.out.println(contentType); boolean flag = false; if(!contentType.equals("image/jpg")){ addExpertError.put("imageerror", "照片格式不正确,必须为jpg格式!"); request.setAttribute("addExpertError", addExpertError); flag = true; } if(flag){ request.getRequestDispatcher("/error.jsp").forward(request, response); } expertphoto = fileName.substring(fileName.lastIndexOf("//"),fileName.length()); expert.setExpertPhoto(expertphoto); FileOutputStream fos=new FileOutputStream(request.getRealPath("/images/")+"//"+ fileName.substring(fileName.lastIndexOf("/"),fileName.length())); //如果上传文件已经在内存中 if(item.isInMemory()){ fos.write(item.get()); }else{ //获取文件的输入流 InputStream is=item.getInputStream(); byte[] buffer=new byte[1024]; int len; while((len=is.read(buffer))>0){ fos.write(buffer, 0, len); } is.close(); fos.close(); } } } //更新专家信息 ExpertDao expertDao = new ExpertDao(); if(!expertDao.addExpertInfo(expert)){ addExpertError.put("expertinfoupdateerror", "更新信息时发生未知错误"); request.setAttribute("addExpertError", addExpertError); request.getRequestDispatcher("/error.jsp").forward(request, response); }else{ request.getRequestDispatcher("/success.jsp").forward(request, response); } }