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/wzjwxx.html