关于更新用户信息丢失头像的问题

本人在设置头像上传更新的时候,发现有个更新的BUG,上传好之后也能显示出来,但是这个时候点击更新,没有重新上传图片,就出来了头像丢失的问题。后来发现controller判断头像是否为空出现了问题
原本判断是 if(file!=null),原本是想判断file是否为空就可以知道修改的时候有没有更改头像,如果不为空则证明更改了头像,那么后面就是添加头像的操作了。
然而关键就在这里,其实这个判断有问题,这样判断是没法判断出file到底改变了没有,也就是说即使没有更改头像,它也会认为这个地方不为空。
所以我就更改了这个判断语句,if(!file.isEmpty())同时sql语句也使用了动态sql语句

<update id="update" parameterType="User">
        update user set 
        u_password=#{uPassword},
        u_name=#{uName},
        u_tel=#{uTel},
        u_email=#{uEmail},
        <if test="uPhoto!=null">u_photo=#{uPhoto}</if>
        where u_id=#{uId}
    </update>

附controller修改头像的源码:

@RequestMapping(value="/user/update.action",method={RequestMethod.POST})
    public String update(User user,HttpServletRequest request,
            @RequestParam("photo") MultipartFile file){
        //1.判断上传的头像是否为空
        if(!file.isEmpty()){
            //头像上传
            //2、指定上传目录
            String str=request.getServletContext().getRealPath("//upload//user");
            System.out.println(str);
            //3、如果路径不存在,创建此路径
            File path=new File(str);
            if(!path.exists()){
                path.mkdirs();
            }
            //4、获取名字
            String name=file.getOriginalFilename();
            //5、防止名字重复
            name=UUID.randomUUID()+name;
            //6、拼接路径      路径加名字  文件
            File desPath= new File(path, name);
            //7、文件上传
            try {
                                            //要上传的路径 , 文件-------字节数组
                FileUtils.writeByteArrayToFile(desPath, file.getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //8、把改路径设置到user里面数据库里D://xxxx//upload//user/xx.jpg
            user.setuPhoto("user//"+name);
            }
        userService.update(user);
        //数据在session中,修改之后需要刷新session里面的值
        User user1=userService.findUser_Class(user.getuId());
        request.getSession().setAttribute("user_class", user1);
        return "kstj/kstj";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值