HttpClient 并从服务器响应中提取音频
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想下载mp3,我认为最简单的方法是:
现在你有了实体并且可以调用entity.getContent();这给你一个 inputStream ,现在你可以用你想要的每种方法保存这个流,当然你需要 mime 类型和文件名来保存你的文件。如果您对文件名和 mime 类型有疑问,请告诉我添加一些示例代码。
If you want download mp3 I Think easiest way is :
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.
您收到需要首先解析的 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: