同时调用上传方法时是需要带_distType参数的,该参数值要与具体IUploadHandler的实现类的类名一样,例如:上传头像就需要将 _distType = _avatar参数带过来。这样UploadController就知道具体用哪个实现类来处理。
2、修改头像有了前面的上传模块,对于修改头像就简单多了,首先需要实现上传头像的实现类,即UploadAvatarHandler类,代码在上方已经罗列了此处省略。
加载出修改头像页面的核心的如下:
/** * 加载出修改头像页面 * * @return */ @RequestMapping(value = "/user/avatar", method = RequestMethod.GET) public String avatar(HttpSession session, Model model) { // session中的信息 User sessionUser = (User) session.getAttribute(Const.SESSION_USER); // 从数据库中获取用户信息 User user = userService.getById(sessionUser.getUserId()); model.addAttribute("user", user); return Const.BASE_INDEX_PAGE + "auth/user/avatar"; }修改头像,运用了fullAvatarEditor插件,所以核心的前台代码如下:
<script type="text/javascript"> swfobject.addDomLoadEvent(function () { var swf = new fullAvatarEditor("${rc.contextPath}/static/plugins/fullAvatarEditor/fullAvatarEditor.swf", "${rc.contextPath}/resources/plugins/fullAvatarEditor/expressInstall.swf", "swfContainer", { id: 'swf', upload_url: '${rc.contextPath}/upload?_distType=_avatar', //上传接口 method: 'post', //传递到上传接口中的查询参数的提交方式。更改该值时,请注意更改上传接口中的查询参数的接收方式 src_upload: 0, //是否上传原图片的选项,有以下值:0-不上传;1-上传;2-显示复选框由用户选择 avatar_box_border_width: 0, avatar_sizes: '150*150', avatar_sizes_desc: '150*150像素', avatar_field_names: '_uploadFile' }, function (msg) { console.log(msg); switch (msg.code) { case 1 : break; case 2 : document.getElementById("upload").style.display = "inline"; break; case 3 : if (msg.type == 0) { } else if (msg.type == 1) { alert("摄像头已准备就绪但用户未允许使用!"); } else { alert("摄像头被占用!"); } break; case 5 : setTimeout(function () { window.location.href = window.location.href; }, 1000); break; } } ); document.getElementById("upload").onclick = function () { swf.call("upload"); }; }); </script>注意:
里面一个upload_url参数就是写上传接口的,上述中为:
upload_url: '${rc.contextPath}/upload?_distType=_avatar'正如在前面讨论的一样的,需要带上 _distType参数
页面效果如下:
注意在回显图片时,需要加上如下配置:
/** * 静态资源配置 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 映射头像图片 String avatarPath = configCache.get(Config.CONFIG_IMG_AVATAR_PATH).getConfigValue(); if (!avatarPath.endsWith("http://www.likecs.com/")) { avatarPath += "http://www.likecs.com/"; } registry.addResourceHandler("/img/avatar/**").addResourceLocations("file:" + avatarPath); } 3、修改密码修改密码功能相对简单,页面效果如下:
此处就只列出修改密码的核心逻辑,即:
/** * 修改密码 * * @param request * @param session * @param model * @return */ @RequestMapping(value = "/user/password", method = RequestMethod.POST) @ResponseBody public Result password(HttpServletRequest request, HttpSession session) { Result result = new Result(); try { // 获取登录信息 User tempUser = (User) session.getAttribute(Const.SESSION_USER); String userId = tempUser.getUserId(); // 接收参数 String password = request.getParameter("password"); String newPwd = request.getParameter("newPwd"); String confirmNewPwd = request.getParameter("confirmNewPwd"); if (StringUtils.isEmpty(password) || StringUtils.isEmpty(newPwd) || StringUtils.isEmpty(confirmNewPwd)) { throw new TipException("缺少必要请求参数"); } if (!newPwd.equals(confirmNewPwd)) { throw new TipException("两次输入的新密码不相等"); } // 获取用户信息 User user = userService.getById(userId); if (!user.getPassword().equals(StringUtil.md5(password))) { throw new TipException("旧密码输入不正确"); } // 修改密码 user.setPassword(StringUtil.md5(newPwd)); boolean flag = userService.updateById(user); if (!flag) { throw new TipException("修改密码失败"); } result.setCode(Result.CODE_SUCCESS); result.setMsg("修改成功"); } catch (TipException e) { result.setCode(Result.CODE_EXCEPTION); result.setMsg(e.getMessage()); } catch (Exception e) { log.error("修改密码失败", e); result.setCode(Result.CODE_EXCEPTION); result.setMsg("修改密码失败"); } return result; } 关注我