static String apiKey="apikey";
static String xparamOrgin="{ \"auf\":\"audio/L16;rate=16000\",\"aue\": \"lame\",\"voice_name\":\"xiaoyan\",\"speed\":\"50\",\"volume\":\"50\",\"pitch\":\"50\",\"engine_type\":\"intp65\",\"text_type\":\"text\"}";
static String xparam = Base64.encoder().encodeToString(xparamOrgin.getBytes());
public static void main(String[]args){
System.out.println(post("hello"));
}
public static String post(String txt){
String currentTimeMillis =System.currentTimeMillis()/1000L+"";
String checkSum =null;
try {
checkSum=md5(apiKey+currentTimeMillis+xparam);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Map<String,String> headermap =new HashMap<>();
headermap.put("X-Appid","appId");
headermap.put("X-CurTime",currentTimeMillis);
headermap.put("X-Param",xparam);
headermap.put("X-CheckSum",checkSum);
headermap.put("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
String url="http://api.xfyun.cn/v1/service/v1/tts";
Map<String,Object>paramMap =new HashMap<>();
try {
// paramMap.put("text", URLEncoder.encode(txt, "utf-8"));
paramMap.put("text", txt);
} catch (Exception e) {
e.printStackTrace();
}
try {
post(url,paramMap,null,null,headermap,"");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String md5(String data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data.getBytes());
StringBuffer buf = new StringBuffer();
byte[] bits = md.digest();
for(int i=0;i<bits.length;i++){
int a = bits[i];
if(a<0) a+=256;
if(a<16) buf.append("0");
buf.append(Integer.toHexString(a));
}
return buf.toString();
}
/**
* post 方法
*
* @param targetUrl
* 目标地址
* @param parameter
* parameter里面的值可以为File,File[],String,String[]
* @param charset 编码
* @param refer 来源
* @return
* @throws Exception
*/
public static String post(String targetUrl, Map<String, Object> parameter, String charset,
String refer,Map<String, String> headerMap,String path) throws Exception
{
URI uri = new URI(targetUrl);
CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore)
.setDefaultHeaders(createHeaders(uri, refer)).setRetryHandler(retryHandler)
.addInterceptorFirst(gzipInterceptor).setRedirectStrategy(new DefaultRedirectStrategy()).build();
String res = null;
HttpPost post = new HttpPost(uri);
try
{
// 处理表单中提交的参数
HttpEntity entity;
Charset textCharset = charset == null ? Charset.forName(encoding) : Charset.forName(charset);
for (String headerName : headerMap.keySet()) {
if ("Content-Length".equalsIgnoreCase(headerName)) {
continue;
}
String headerValue = headerMap.get(headerName);
post.addHeader(headerName, headerValue);
}
entity = new UrlEncodedFormEntity(doCommPart(parameter), textCharset);
post.setEntity(entity);
dealOutPutMp3Response(client.execute(post),path);
}
finally
{
post.releaseConnection();
client.close();
}
return null;
}
/**
* 处理HttpResponse内容,解决可能的乱码问题
*
* @param response
* @return
* @throws Exception
*
*/
private static void dealOutPutMp3Response(HttpResponse response,String path) throws Exception
{
HttpEntity entity = response.getEntity();// unCompressEntity(response);
ContentType contentType = ContentType.getOrDefault(entity);
InputStream in = entity.getContent();
BufferedInputStream reader = new BufferedInputStream(in);
OutputStream outs = new FileOutputStream(path);
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();
in.close();
EntityUtils.consume(entity);
}
apiKey appID均在控制台 项目里
IP白名单 必须添加本机的外网ip地址,否则视为不合法访问请求。