科大讯飞 java Web api语音生成和语音识别参考

对此感兴趣的可以加群261074724 

 文档地址

  http://doc.xfyun.cn/rest_api/%E6%8E%A5%E5%8F%A3%E6%A6%82%E8%BF%B0.html

 

 

{ "code":"10105", "desc":"illegal access|illegal client_ip", "data":"", "sid":"xxxxxx" }

 

出现这个错误需要把ip加到后台上面地址底部有说明

package com.test;

 


import java.io.BufferedInputStream; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import java.net.HttpURLConnection;
import java.net.URL; 


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils; 
/**
 * commons-codec-1.9.jar

 

 

 * 科大讯飞语音合成 

 

 * @author lilin

 *
 */
public class Test {
     /**
      * 科大讯飞语音识别写入参考
      * https://github.com/IflytekAIUI/DemoCode/blob/master/webapi/java/Iat.java
      */
final static String APPID="**",APPKEY="**";
final static String url = "http://api.xfyun.cn/v1/service/v1/tts/";
public static void sendPost( String text)throws Exception{
    Base64 base64 = new Base64(); 
        try {
            URL httpUrl  = new URL(url);
            String param = "{\"auf\":\"audio/L16;rate=16000\",\"aue\":\"lame\",\"voice_name\":\"xiaoyan\",\"speed\":\"50\",\"volume\":\"80\",\"pitch\":\"50\",\"engine_type\":\"intp65\",\"text_type\":\"text\"}";
String paramBase64=base64.encodeAsString( param.getBytes("UTF-8")); 
//建立连接 
            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setRequestMethod("POST");
          
           String currentTimeMillis =System.currentTimeMillis() / 1000L + "";
           String md5Hex = DigestUtils.md5Hex( (APPKEY + currentTimeMillis + paramBase64).getBytes());
           
conn.setRequestProperty("X-CurTime", currentTimeMillis);
            conn.setRequestProperty("X-Param",paramBase64);  
            conn.setRequestProperty("X-Appid",APPID);  
conn.setRequestProperty("X-CheckSum", md5Hex);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); 
                 
// 设置请求 body
conn.setDoOutput(true);
conn.setDoInput(true);

//设置连接超时和读取超时时间
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);

            conn.connect();
            //POST请求
            OutputStream out = conn.getOutputStream(); 
            out.write(("text="+text).getBytes());
            out.flush()
            ;
            //读取响应
            BufferedInputStream reader = new BufferedInputStream(  conn.getInputStream());
            String headerField = conn.getHeaderField("Content-type");
            System.out.println(headerField);
            out.close();
           if( headerField.equalsIgnoreCase("text/plain") ){
              System.out.println("错误");
            }else{
            OutputStream outs = new FileOutputStream("C:/Users/lilin/Desktop/a.mp3");
            int size = 0, len = 0;
                byte[] buf = new byte[1024];
                while ((size = reader.read(buf)) != -1) {
                     len += size;  outs.write(buf, 0, size); 
            }  
            outs.close();
            reader.close(); 
            }
        conn.disconnect();
            
        } catch (Exception e) { e.printStackTrace();   }  
   
}

 
public static void main(String[] args) {
try { 
 
sendPost("你好啊美女");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

你可能感兴趣的:(java)