网页验证码后端服务Java类

package com.You.controller;

import com.google.gson.Gson;
import com.You.pojo.Staff;
import com.You.service.StaffService;
import com.You.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/information")
public class InformationController {

    @Autowired
    StaffService staffService;

    //用户名做了唯一校验 不会产生重复情况
    //接口1.跳转个人信息页面、进行个人信息回显
    //回显逻辑:根据用户名username来进行查询回显操作
    @RequestMapping("/toInformation")
    public String toInformation(@RequestParam("username")String username,
                                Model model){
        //暂时使用测试数据
        //username="You";


        //根据用户名查询数据
        Staff staff=staffService.getStaffByName(username);
        //封装给前端
        model.addAttribute("sf",staff);
        return "information";
    }

    //文件上传
    @RequestMapping("/uploadImg")
    @ResponseBody
    public String uploadImg(@RequestParam("file") MultipartFile file,
                            @RequestParam("userId")Long userId,
                            HttpServletRequest request){

        System.out.println("文件上传中···");
        //1.上传文件到服务器 返回存放的路径
        // 上传图片
        String imgUrl = UploadUtils.uploadImg(file,request);
        System.out.println(imgUrl);

        //2.根据上传的文件路径,在数据中进行修改对应的路径
        //封装当前对象的参数
        //创建员工对象
        Staff staff=new Staff();
        staff.setUserId(userId);
        staff.setUserPhotoUrl(imgUrl);

        //修改数据
        int row = staffService.updateStaff(staff);
        //定义返回的数据的封装、map数据
        Map<String, Object> result = new HashMap<>();

        //判断新增
        if (row>0){
            result.put("flag",1);
            result.put("msg","头像文件上传成功!");
        }else {
            result.put("flag",0);
            result.put("msg","头像文件上传失败!");
        }
        return new Gson().toJson(result);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值