java 使用 HttpURLConnection 发送二进制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26078344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:15:38  来源:igfitidea点击:

Sending binary data with HttpURLConnection

javapostgoogle-apihttpurlconnectionbinary-data

提问by hnnn

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.

我想使用谷歌语音 api,我发现这个https://github.com/gillesdemey/google-speech-v2/一切都解释得很好,但是我试图将它重写为 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");

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 \

but httpurlconnection class has no method for attaching binary data.

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

回答by Simon Fischer

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

但是getOutputStream()您可以将数据写入其中。您可能还想致电setDoOutput(true).

回答by Italo Borssatto

The code below works for me. I just used commons-ioto simplify, but you can replace that:

下面的代码对我有用。我只是用来commons-io简化,但你可以替换它:

    URL url = new URL("https://www.google.com/speech-api/v2/recognize?lang=en-US&output=json&key=" + key);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "audio/x-flac; rate=16000");
    IOUtils.copy(new FileInputStream(flacAudioFile), conn.getOutputStream());
    String res = IOUtils.toString(conn.getInputStream());

回答by Udit Saini

Use?multipart/form-data?encoding for mixed POST content (binary and character data)

对混合 POST 内容(二进制和字符数据)使用 multipart/form-data 编码

//set connection property
connection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + <random-value>);

PrintWriter writer = null;
OutputStream output = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output, charset), true);


// Send binary file.
writer.append("--" + boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append("\r\n");
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName()).append("\r\n");
writer.append("Content-Transfer-Encoding: binary").append("\r\n");
writer.append("\r\n").flush();