在线语音验证码

一、基本数据准备

1.1 语音材料

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

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 所需依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

二、前后端实现

2.1 页面实现

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<audio src="/data/end.mp3?num=<%=Math.random()%>"  autoplay="autoplay" ></audio>
<a href="/code">获取语音验证码</a>
</body>
</html>

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<FileInputStream> 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<FileInputStream> 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;
    }

猜你喜欢

转载自blog.csdn.net/qq_35813653/article/details/82534551