用HttpURLConnection发送二进制数据 [英] Sending binary data with HttpURLConnection

查看:281
本文介绍了用HttpURLConnection发送二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用谷歌语音API,我发现这个 https:// github。 com / gillesdemey / google-speech-v2 / ,其中一切都很好解释,但是我试图把它改写成java。

i want to use google speech api, i've found this https://github.com/gillesdemey/google-speech-v2/ where everything is explained well, but and im trying to rewrite it into java.

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添加到请求中。在示例中它的行

now im lost... i guess i need to add the bytearray into the request. in the example its line

--data-binary @audio/good-morning-google.flac \

但是httpurlconnection类没有附加二进制数据的方法。

but httpurlconnection class has no method for attaching binary data.

推荐答案

但它有 getOutputStream(),您可以在其中编写数据。您可能还想调用 setDoOutput(true)

But it has getOutputStream() to which you can write your data. You may also want to call setDoOutput(true).

这篇关于用HttpURLConnection发送二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆