【问题标题】:How to handle response Content Type Audio when accessing REST API?访问 REST API 时如何处理响应内容类型音频?
【发布时间】:2016-03-20 01:17:51
【问题描述】:

我正在使用Voice RSS 为我的 java 应用程序进行文本到语音的翻译。我过去使用过像 RESTY 这样的客户端来处理简单的 json 请求,这些请求很舒服。但在这种情况下,服务器(语音 RSS)将内容类型作为音频返回,我不确定如何将其作为 java 客户端处理和解包。任何帮助将不胜感激

谢谢 卡提克

【问题讨论】:

    标签: rest content-type rest-client


    【解决方案1】:

    我发现您可以使用 Url 和 BufferedInputStream 将来自 Web 服务的响应下载为字节数组或输出流,您可以将其保存为任何内容。

    InputStream in = null;
            ByteArrayOutputStream outputStream = null;
            byte[] byteArray = null;
            try {
                URL link = new URL("YOUR_URL");
                in = new BufferedInputStream(link.openStream());
                outputStream = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int n = 0;
                while (-1 != (n = in.read(buf))) {
                    outputStream.write(buf, 0, n);
                    }
                byteArray = outputStream.toByteArray();
            }catch (Exception ex){
                throw ex;
            }finally {
                if(in != null) in.close();;
                if(outputStream != null) outputStream.close();
            }
    

    【讨论】:

      猜你喜欢
      • 2015-07-23
      • 1970-01-01
      • 2013-06-06
      • 2018-01-30
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      相关资源
      最近更新 更多