用httppost上传二进制数据

3pvhb19x  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(447)

我正在尝试使用ApacheHttpPost从java客户机访问IBMWatson restful接口(语音到文本),但未能正确上载二进制.wav输入文件。
以下“curl”命令工作正常,产生正确的结果:

curl -u "user:password" -H "content-type: audio/wav" --data-binary @"newfile.wav" "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize" -X POST

下面的java客户端打算复制上述功能:

public void speech2text(String user, String password, String file_name) {

  try {

    String ulr_string = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize";

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(ulr_string);
    httpPost.addHeader(BasicScheme.authenticate(
     new UsernamePasswordCredentials(user, password), "UTF-8", false));

    httpPost.addHeader("content-type", "audio/wav");
    httpPost.addHeader("content-type", "multipart/form-data");
    // httpPost.addHeader("transfer-encoding", "chunked");

    File input_file = new File(file_name);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addBinaryBody("upfile", input_file, ContentType.DEFAULT_BINARY, "c:\\Temp\\newfile.wav");
    HttpEntity entity = builder.build();
    httpPost.setEntity(entity);

    System.out.println("executing request " + httpPost.getRequestLine());
    Header headers[] = httpPost.getAllHeaders();
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity resEntity = response.getEntity();

    System.out.println(response.getStatusLine());
    if (resEntity != null) {
      System.out.println(EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
      EntityUtils.consume(resEntity);
    }

    httpClient.getConnectionManager().shutdown();

} catch (MalformedURLException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

}
但请求失败,返回:

HTTP/1.1 400 Bad Request
{
  "code_description": "Bad Request", 
  "code": 400, 
  "error": "unable to transcode data stream audio/wav -> audio/x-float-array "
}

watson的api需要对大文件进行分块传输编码,但我正在使用的示例非常小。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题