我想使用谷歌语音API,我发现这一切都解释得很好,但即时尝试将其重写到Java。用HttpURLConnection发送二进制数据
File filetosend = new File(path);
byte[] bytearray = Files.readAllBytes(filetosend);
URL url = new URL("https://www.google.com/speech-api/v2/recognize?output="+outputtype+"&lang="+lang+"&key="+key);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//method
conn.setRequestMethod("POST");
//header
conn.setRequestProperty("Content-Type", "audio/x-flac; rate=44100");
现在我失去了......我想我需要将bytearray添加到请求中。在该示例中它的行
--data-binary @audio/good-morning-google.flac \
但httpurlconnection类没有附加二进制数据的方法。
在第2和第10行究竟代表边界? – hnnn 2014-09-27 20:12:04
OP或相应的API文档在哪里要求多部分? – 2014-09-27 20:16:03
@hnnn边界是以毫秒为单位的当前时间的十六进制(基数为16)表示。根据API,它允许嵌套多部分流的单通处理。访问http://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/MultipartStream.html – 2014-09-27 20:32:45