阿里云Oss(图片上传),SMS(发送短信验证)

文件上传
之前做的文件上传是将文件上传到tomcat本地硬盘上,然后通过虚拟路径访问该文件。
但是如果在tomcat集群环境,这种方案肯定是不行的,所以需要用分布式的文件系统来存取文件。
这里我们使用阿里云的oss服务,来存储文件数据(图片、小视频、js、css.html)

1.阿里云登陆注册

https://help.aliyun.com/learn/learningpath/oss.html

创建bucket

获取四个关键属性

AccessKey LTAI5tBxZTXL91xi8hBekESS
AccessKey Secret YXhkQIfHeyfhxWa9lhTbTfCIfJ3EKy
服务器域名Endpoint oss-cn-beijing.aliyuncs.com
数据仓库 bucket zkp-01

2.OSS代码实现

  1. 导包
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.1.0</version>
        </dependency>
  1. 引入工具类OSSClientUtil修改属性为自己的bucket
    // endpoint  域名 机房
    private String endpoint = "oss-cn-beijing.aliyuncs.com";
    // accessKey  用户的Id
    private String accessKeyId = "LTAI5tBxZTXL91xi8hBekESS";
    // 用户的秘钥
    private String accessKeySecret = "YXhkQIfHeyfhxWa9lhTbTfCIfJ3EKy";
    // 仓库
    private String bucketName = "zkp-01";
    // 文件存储目录
    private String filedir = "images/";
  1. 在controller当中调用
    //异步上传照片
    @RequestMapping("/doAddPhoto")
    public @ResponseBody boolean doAddPhoto(@RequestParam("file") MultipartFile file,HttpSession session, String picName) throws IOException {
        User user = (User)session.getAttribute("user");
        if (!file.isEmpty()){//文件非空,上传到服务器
            //使用时创建一个,多例模式
            OSSClientUtil ossClientUtil = new OSSClientUtil();
            ossClientUtil.uploadFile2OSS(file.getInputStream(),file.getOriginalFilename());
            //获取网络地址,通过文件名称
            String url = ossClientUtil.getUrl(file.getOriginalFilename());
            //创建用户相片对象,添加新信息
            UserProfilePhoto userPhoto = new UserProfilePhoto();
            userPhoto.setPicUrl(url);
            userPhoto.setPicName(picName);
            userPhoto.setUserId(user.getId());
            PhotoService.insert(userPhoto);
            return true;
        }else {
            return false;
        }
    }

3.SMS(发送短信,实现手机号短信登陆)

  1. 导包
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>
  1. 引入工具类AliSmsUtil,修改部分属性
//产品域名,开发者无需替换,以下常量基本不变
private static final String DOMAIN = "dysmsapi.aliyuncs.com";

private static final String REGION_ID = "cn-hangzhou";
private static final String PRODUCT="Dysmsapi";

public static final String ACCESS_KEY_ID = "LTAI4GHFHhok7nJ7cA77GSw9";
public static final String ACCESS_SECRET = "mGr8Q4D7mDFksn0VJA31S421M89ncU";
public static final String DEFAULT_SIGNNAME = "ABC商城";
public static final String DEFAULT_TEMPLATE_CODE = "SMS_206420122";
  1. 创建一个线程类SendSmsTask,进行异步调用
public class SendSmsTask implements  Runnable {
    private AliSmsUtil aliSmsUtil;
    private String phone;
    private String code;

    public SendSmsTask(AliSmsUtil aliSmsUtil, String phone,String code) {
        this.aliSmsUtil = aliSmsUtil;
        this.phone = phone;
        this.code=code;
    }

    @Override
    public void run() {
        try {
            aliSmsUtil.sendValidateCodeMessage2AliyunSms(phone, code);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  1. 具体业务
//短信验证码登陆
    @RequestMapping("/msgLogin")
    public @ResponseBody boolean msgLogin(String code,HttpSession session){

        //获得用户验证码,与redis中的进行比对
        String codes =(String) redisTemplate.boundValueOps("msg").get();
        System.out.println(code);
        System.out.println(codes);
        if (code.equals(codes)){//用户登陆成功
            User user = userService.selectByPrimaryKey(3);
            List<Menu> menuList = menuService.findMenuByUserId(user.getId());  
            session.setAttribute("menuList",menuList);
            session.setAttribute("user",user);
            return true;
        }else {
            return false;
        }
    }

    //发送验证码
    @RequestMapping("/sendMsg")
    public @ResponseBody ResponseVo sendMsg(String phone) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode("1");
        String code = "";
        //生成随机数
        Random random = new Random();
        for (int i=1;i<=6;i++){
            code += ""+ random.nextInt(10);
        }
        //将验证吗放入redis
        redisTemplate.boundValueOps("msg").set(code);
        System.out.println("code="+code);
        SendSmsTask sendSmsTask = new SendSmsTask(new AliSmsUtil(),phone,code);
        sendSmsTask.run();
        return responseVo;
    }
posted @ 2021-11-06 13:12  2333gyh  阅读(785)  评论(0编辑  收藏  举报