一次对接三方外呼系统的时候,在解析对方返回的通话记录的时候,其中录音返回的是一个URL地址,可以通过浏览器访问,然后跳转到一个以.mp3结尾的地址去听取录音。
三方返回的录音地址:
https://as01.nxcloud.com/record/83fe5634-a34c-7a0f-5db8-b1d45a162af0
经对三方提供的URL地址请求返回参数打印如下:
Keep-Alive--->[timeout=5, max=100]
null--->[HTTP/1.1 302 Found]
Server--->[Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16 mod_wsgi/4.6.2 Python/3.6]
Connection--->[Keep-Alive]
Content-Length--->[0]
Date--->[Sat, 15 Feb 2020 07:10:51 GMT]
Content-Type--->[text/html; charset=UTF-8]
Location--->[http://cs01.nxcloud.com:7180/1815001/349411f0-86dc-4df1-a4e3-d7ca6d6f1bc1.mp3]
X-Powered-By--->[PHP/5.4.16]
-1
-1
[B@24a35978
其实真实的录音地址是在Location中,故首先得获取该Location中的地址,然后在对Location地址解析。
工具类:
package com.github.collection.common.util;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;
public class HttpRequestUtil {
public static void main(String[] args) {
try {
String url = "https://as01.nxcloud.com/record/83fe5634-a34c-7a0f-5db8-b1d45a162af0";
String urlNameString = url;
//忽略安全证书
trustAllHosts();
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setConnectTimeout(30 * 1000);
// 建立实际的连接
connection.connect();
String location = connection.getHeaderField("Location");
System.out.println(location);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void trustAllHosts() {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
}};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
e.printStackTrace();
}
}
}
之后可对该网路录音转换成byte[]
/**
* 读取录音文件流
* @param url
* @return
* @throws Exception
*/
public static byte[] readAndSave(String url)throws Exception{
System.out.println("Starting.");
URL u = new URL(url);
byte[] buffer = new byte[1024*8];
int read;
int ava = 0;
long start = System.currentTimeMillis();
BufferedInputStream bin = new BufferedInputStream(u.openStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((read=bin.read(buffer))>-1){
bos.write(buffer, 0, read);
ava+=read;
long speed = ava/(System.currentTimeMillis()-start);
System.out.println("Download: "+ava+" byte(s)"+" avg speed: "+speed+" (kb/s)");
}
bos.flush();
bos.close();
System.out.println("Done. size:"+ava+" byte(s)");
return bos.toByteArray();
}
然后可以存进阿里云OSS中。