JSP+Servlet实现文件上传随处事器成果(2)

修改用户时留意思量图片变动和没变动这两种环境,图片变动时要先获取原图片并删除其在处事器上的图片,再添加新图片随处事器;图片不变动时则无需更新图片路径。

// 改 public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("update要领被挪用"); HttpSession session = request.getSession(); // 获取数据 int id = (int)session.getAttribute("id"); String username = null; String password = null; String sex = null; Date birthday = null; String address = null; String saveFileName = null; String picturePath = null; IUserService iUserService = new UserServiceImpl(); // 获得表单是否以enctype="multipart/form-data"方法提交 boolean isMulti = ServletFileUpload.isMultipartContent(request); if (isMulti) { // 通过FileItemFactory获得文件上传的工具 FileItemFactory fif = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(fif); try { List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { // 判定是否是普通表单控件,可能是文件上传表单控件 boolean isForm = item.isFormField(); if (isForm) {// 是普通表单控件 String name = item.getFieldName(); if ("sex".equals(name)) { sex = item.getString("utf-8"); System.out.println(sex); } if ("username".equals(name)) { username = item.getString("utf-8"); System.out.println(username); } if ("password".equals(name)) { password = item.getString("utf-8"); System.out.println(password); } if ("birthday".equals(name)) { String birthdayStr = item.getString("utf-8"); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd"); try { birthday = sdf.parse(birthdayStr); } catch (ParseException e) { e.printStackTrace(); } System.out.println(birthday); } if ("address".equals(name)) { address = item.getString("utf-8"); System.out.println(address); } if ("picturePath".equals(name)) { picturePath = item.getString("utf-8"); System.out.println(picturePath); } } else {// 是文件上传表单控件 // 获得文件名 xxx.jpg picturePath = item.getName(); if (picturePath != "") {// 有选择要上传的图片 // 获得文件名的扩展名:.jpg String extendedName = picturePath.substring( picturePath.lastIndexOf("."),// 截取从最后一个'.'到字符串竣事的子串。 picturePath.length()); // 把文件名称重定名为全球独一的文件名 String uniqueName = UUID.randomUUID().toString(); saveFileName = uniqueName + extendedName;// 拼接路径名 // 获得上传随处事器上的文件路径 // C:\\apache-tomcat-7.0.47\\webapps\\CommonhelloWorldServlet\\upload\\xx.jpg String uploadFilePath = request.getSession() .getServletContext().getRealPath("upload/"); File saveFile = new File(uploadFilePath, saveFileName); // 把生存的文件写出随处事器硬盘上 try { item.write(saveFile); } catch (Exception e) { e.printStackTrace(); } // 3、挪用逻辑层 API // 按照id查询用户并获取其之前的图片 User user = iUserService.getUserById(id); String oldPic = user.getPicturePath(); String oldPicPath = uploadFilePath + "\\" + oldPic; File oldPicTodelete = new File(oldPicPath); oldPicTodelete.delete();// 删除旧图片 } } } } catch (NumberFormatException e) { e.printStackTrace(); } catch (FileUploadException e) { e.printStackTrace(); } } System.out.println(id + "\t" + username + "\t" + password + "\t" + sex + "\t" + address + "\t" + picturePath + "\t" + birthday); // 2、封装数据 User user = new User(id, username, password, sex, birthday, address, saveFileName); if (iUserService.update(user) > 0) { System.out.println("修改数据乐成!"); List<User> users = new ArrayList<User>(); users = iUserService.listAll(); session.setAttribute("users", users); // 4、节制跳转 response.sendRedirect("UserServlet?action=getPage"); } else { System.out.println("修改数据失败!"); PrintWriter out = response.getWriter(); out.print("<script type='text/javascript'>"); out.print("alert('修改数据失败!请重试!');"); out.print("</script>"); } }

删除的话就较量简朴了,直接获取原图片路径并删除,则原图片在处事器上被删除。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsdddf.html