我必须使用http api调用将wave文件发送到wit.ai.
$ curl -XPOST 'https://api.wit.ai/speech?v=20141022' \
-i -L \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: audio/wav" \
--data-binary "@sample.wav"
我正在使用Java,但我必须使用Java发送此请求,但我无法在Java中正确转换此curl请求。我无法理解什么是-i和-l以及如何在Java的后期请求中设置data-binary。
这是我到目前为止所做的
public static void main(String args[])
{
String url = "https://api.wit.ai/speech";
String key = "token";
String param1 = "20170203";
String param2 = command;
String charset = "UTF-8";
String query = String.format("v=%s",
URLEncoder.encode(param1, charset));
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty ("Authorization","Bearer"+ key);
connection.setRequestProperty("Content-Type", "audio/wav");
InputStream response = connection.getInputStream();
System.out.println( response.toString());
}
这是编写sample.wav
连接的输出流的方法,请注意,以下代码段中的和之间固定了一个空格:Bearer
token
public static void main(String[] args) throws Exception {
String url = "https://api.wit.ai/speech";
String key = "token";
String param1 = "20170203";
String param2 = "command";
String charset = "UTF-8";
String query = String.format("v=%s",
URLEncoder.encode(param1, charset));
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty ("Authorization","Bearer " + key);
connection.setRequestProperty("Content-Type", "audio/wav");
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
FileChannel fileChannel = new FileInputStream(path to sample.wav).getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while((fileChannel.read(byteBuffer)) != -1) {
byteBuffer.flip();
byte[] b = new byte[byteBuffer.remaining()];
byteBuffer.get(b);
outputStream.write(b);
byteBuffer.clear();
}
BufferedReader response = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = response.readLine()) != null) {
System.out.println(line);
}
}
PS:我已经成功测试了上面的代码,它很吸引人。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句