//生成6位验证码//上传图片压缩

//生成6位验证码

 String code = RandomStringUtils.randomNumeric(6);
//遍历list,用函数式编程进行业务逻辑处理
 parkStaff.getEnterpriseIds().forEach(enterpriseId ->{

            // 判断同一企业下手机号是否存在
            ParkStaff phoneCheck = new ParkStaff();
            phoneCheck.setEnterpriseId(enterpriseId);
            phoneCheck.setPhone(parkStaff.getPhone());
            phoneCheck.setState("1");

            List<ParkStaff> parkStaffs = selectParkStaffList(phoneCheck);
            if(parkStaffs.size() == 0){
                // 手机号不存在
                // 添加数据
                ParkStaff ps = new ParkStaff();
                parkStaff.setEnterpriseId(enterpriseId);
                List<ParkStaff> staffs = selectParkStaffList(ps);
                List<String> collect = staffs.stream().map(ParkStaff::getCode).collect(Collectors.toList());

                String code = null;
                while (true){
                    int rado= (int)((Math.random()*9+1)*10000);
                    code = "800" + String.valueOf(rado);
                    if(!collect.contains(code)){
                        break;
                    }
                }
                parkStaff.setEnterpriseId(enterpriseId);
                parkStaff.setCode(code);
                parkStaff.setCreateTime(DateUtils.getNowDate());
                parkStaffMapper.insertParkStaff(parkStaff);
            }

        });

//上传图片压缩

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>
  /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            MultipartFile oldMultipartFile = file;
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            //上传图片的话就压缩
            String oldfileName = file.getName();
            String originalFilename = file.getOriginalFilename();
            String fileType = originalFilename.substring(originalFilename.lastIndexOf("."));
            if (".jpg".equalsIgnoreCase(fileType) || ".png".equalsIgnoreCase(fileType)) {
                //计算缩放比例保留四位小数
                double filesize=file.getSize();
                double num=1;
                if(filesize>1060000) {
                    num = 1024 * 1024 / filesize;
                }
                BigDecimal b1 = new BigDecimal(num);
                double quality=b1.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                LocalDateTime now = LocalDateTime.now();
                String absolutePath = filePath+"/"+oldfileName+"-tmp"+fileType;
                File tempFile = new File(absolutePath);
                Thumbnails.of(file.getInputStream())
                        .scale(1f) //图片大小(长宽)压缩比例 从0-1,1表示原图
                        .outputQuality(quality) //图片质量压缩比例 从0-1,越接近1质量越好
                        .toFile(tempFile);
                FileInputStream fileInputStream = new FileInputStream(tempFile);
                file = new MockMultipartFile(oldfileName, originalFilename, fileType, fileInputStream);
                fileInputStream.close();
                boolean success = tempFile.delete();
                log.info("删除临时file success:{}", success);
            }
            //上传文件
            String fileName = FileUploadUtils.upload(filePath, file);
            // 上传并返回新文件名称
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值