在线语音验证码

一、基本数据准备

1.1 语音材料

最开始采用m4a格式的录音,文件能合并,但是无法正常播放

在线语音验证码_第1张图片

1.2  获取绝对路径和生成验证码

  /**
     * 生成验证码
     * @param num
     * @return
     */
    public static String genCode(int num) {
        StringBuffer sf = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            int r = random.nextInt(9);
            sf.append(r);
        }
        return sf.toString();
    }

    /**
     * 获取绝对路径
     * @param filepath
     * @return
     */
    public static String getRealPath(String filepath) {
        String path = ClassLoader.getSystemResource("").getPath();

        if (filepath.endsWith("/") || filepath.endsWith("\\")) {
            return path + filepath;
        }
        return path + "/" + filepath;
    }

1.3 所需依赖

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.apache.commons
            commons-lang3
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
        
        
            javax.servlet
            javax.servlet-api
        
        
            javax.servlet
            jstl
        
        
            taglibs
            standard
            1.1.2
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

二、前后端实现

2.1 页面实现

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title



获取语音验证码

2.2 controller层

/**
 * @author sunyiran
 * @date 2018-09-08
 */
@Controller
@RequestMapping("/code")
public class CodeController {
    @GetMapping
    public String getCode() throws IOException {
        String code = CodeUtils.genCode(4);
        CodeUtils.getCodeM4a(code);
        return "index";
    }
}

         2.3 生成语音文件

 public static File getCodeM4a(String code) throws IOException {
        File file = new File(getRealPath("/static/data/end.mp3"));
        if (file.exists()) {
            file.delete();
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        FileInputStream fis1 = new FileInputStream(getRealPath("/static/data/base.mp3"));

        Vector v = new Vector<>();
        v.add(fis1);

        char[] chars = code.toCharArray();
        for (char name : chars) {
            FileInputStream fis = new FileInputStream(getRealPath("/static/data/"+name+".mp3"));
            v.add(fis);
        }
        Enumeration en = v.elements();
        SequenceInputStream sis = new SequenceInputStream(en);
        int b;
        while((b = sis.read())!= -1)
        {
            fos.write(b);
        }
        sis.close();
        fos.close();
        fis1.close();

        return file;
    }

 

你可能感兴趣的:(后端技术杂述)