Content

JSP+Servlet实现文件上传到服务器功能(jsp+servlet属于什么架构)墙裂推荐

文章摘要

这篇代码主要实现了`update`方法的功能,用于处理文件上传和用户数据更新。代码主要包括以下几个核心部分: 1. **获取表单数据**:通过会话对象获取表单数据,包括用户信息(如用户名、密码、性别、生日、地址、图片路径等)和文件上传。 2. **处理多文件上传**:如果表单是多文件上传形式(`multipart/form-data`),代码会解析文件并处理图片路径,生成唯一文件名并保存到服务器。 3. **删除旧图片**:在处理文件上传时,代码会删除旧的图片文件。 4. **提交用户数据**:调用逻辑层API更新用户数据,包括删除旧图片和保存新图片。 5. **错误处理**:代码中包含多种异常处理,确保程序在异常情况下能够正确报错并提供反馈。 总结来说,该方法负责从客户端接收并处理文件上传请求,同时更新用户数据,并在处理过程中进行错误处理。

 // 改 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\?d\\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>”); } }