HttpClient 并从服务器响应中提取音频

发布于 2024-12-02 12:17:30 字数 752 浏览 2 评论 0原文

我正在使用 Apache HttpClient 连接到服务器以下载 .wav 文件。我在程序中使用 HTTP POST 方法。

服务器正确响应以下标头和正文:

> HTTP/1.1 200 OK\r\n Content-Disposition: attachment;
> filename=saveme1.mp3\r\n Content-Length: 6264\r\n
> Content-Transfer-Encoding: binary\r\n Content-Type: audio/mp3\r\n

现在如何从 HTTP 响应中提取 saveme1.mp3 文件?我正在使用以下代码:

       ResponseHandler<String> responseHandler = new BasicResponseHandler();
       byte[] data = httpclient.execute(httppost, responseHandler).getBytes();

但是,当我将数据写入文件时,我收到了垃圾。

FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
for (int i = 0; i < data.length; i++) 
        fileoutputstream.write(data[i]);

I am using Apache HttpClient to connect to a server for downloading a .wav file. I am using HTTP POST method in my program.

The server correctly responds with the following header and body:

> HTTP/1.1 200 OK\r\n Content-Disposition: attachment;
> filename=saveme1.mp3\r\n Content-Length: 6264\r\n
> Content-Transfer-Encoding: binary\r\n Content-Type: audio/mp3\r\n

How do I now extract the saveme1.mp3 file from the HTTP response? I am using the following code:

       ResponseHandler<String> responseHandler = new BasicResponseHandler();
       byte[] data = httpclient.execute(httppost, responseHandler).getBytes();

However, I am getting garbage when I am writing the data to a file.

FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
for (int i = 0; i < data.length; i++) 
        fileoutputstream.write(data[i]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路终见情 2024-12-09 12:17:30

如果你想下载mp3,我认为最简单的方法是:

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

现在你有了实体并且可以调用entity.getContent();这给你一个 inputStream ,现在你可以用你想要的每种方法保存这个流,当然你需要 mime 类型和文件名来保存你的文件。如果您对文件名和 mime 类型有疑问,请告诉我添加一些示例代码。

If you want download mp3 I Think easiest way is :

HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

Now you have entity and can call entity.getContent(); This give you you a inputStream , now you can save this stream with every method you want , ofcurse you need mime type and filename to save your file. if you have problem with filename and mime type tell me to add some sample code.

作死小能手 2024-12-09 12:17:30

您收到需要首先解析的 MIME 附件。 BasicResponseHandler 仅返回响应字符串,但您需要包含 .mp3 二进制文件的附件正文。您需要执行以下步骤:

You are getting MIME attachment that you need to parse first. The BasicResponseHandler just return the response string, but you need the body of the attachment that contains the binary of your .mp3. You would need to do the following steps:

  • Understand the MIME format. You could skim the Wikipedia Entry for gaining quick familiarity
  • Once you understood, you need to create a MIME Parser. This would basically extract each part of the MIME message especially the body of your attachment. I think there should be something out there that you could reuse. You probably should look MimeMultipart. The only thing that I am not sure about it is whether it handles "binary" encoding in your message.
  • Create your own extension of ResponseHandler that will utilize the MIME Parser that you have in the previous step
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文