java mp3输入流到字节数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4280535/
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
java mp3 inputstream to byte array?
提问by ZolaKt
before you say "Google it", I tried, found a few interesting articles, but nothing worked.
在你说“谷歌它”之前,我试过,找到了一些有趣的文章,但没有任何效果。
I need to convert a mp3 file from a website to a byte stream, that I can later save into a file locally.
我需要将网站上的 mp3 文件转换为字节流,稍后我可以将其保存到本地文件中。
Here is my code (most important parts):
这是我的代码(最重要的部分):
Url url = new Url("someUrl");
URLConnection conn = url.openConnection();
byte[] result = inputStreamToByteArray(conn.getInputStream());
// .... some code here
byteArrayToFile(result, "tmp.mp3");
public byte[] inputStreamToByteArray(InputStream inStream){
InputStreamReader in = new InputStreamReader(inStream):
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int next = inStream.read();
while (next > -1){
baos.write(next);
next = in.read();
}
byte[] result = baos.toByteArray();
baos.flush();
in.close();
return result;
}
public void byteArrayToFile(byte[] byteArray, String outFilePath){
FileOutputStream fos = new FileOutputStream(outFilePath);
fos.write(byteArray);
fos.close()
}
The code compiles without errors. Connection to the url is valid. It sends the right response.
代码编译没有错误。与 url 的连接有效。它发送正确的响应。
The problem is somewhere in the conversion. I also get the new file on disk, after byteArrayToFile(), with appropriate lenght, but I can't play it in any player. It says length 00:00 and will not play.
问题出在转换的某个地方。在 byteArrayToFile() 之后,我还在磁盘上获取了具有适当长度的新文件,但我无法在任何播放器中播放它。它说长度 00:00 并且不会播放。
Btw. I'd like to avoid any 3rd party libs. But if nothing else works...
顺便提一句。我想避免使用任何 3rd 方库。但如果没有其他工作...
回答by Jon Skeet
(The code you've presented wouldn'tcompile without errors, by the way. You haven't got required exception handling, and inputStream
should be InputStream
, just for starters.)
(顺便说一下,您提供的代码不会在没有错误的情况下编译。您还没有获得必需的异常处理,inputStream
应该是InputStream
,仅适用于初学者。)
This is the problem:
这就是问题:
InputStreamReader in = new InputStreamReader(inStream):
You're trying to read from a binary stream, and convert it into text. It isn'ttext. You shouldn't be using anything to do with "Reader" or "Writer" for binary data like an MP3 file.
您正在尝试从二进制流中读取数据,并将其转换为文本。这不是文字。对于 MP3 文件等二进制数据,您不应该使用与“Reader”或“Writer”有关的任何内容。
Here's what your inputStreamToByteArray
method should look like (or use Guavaand its ByteStreams.toByteArray
method):
以下是您的inputStreamToByteArray
方法的外观(或使用Guava及其ByteStreams.toByteArray
方法):
public byte[] inputStreamToByteArray(InputStream inStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = inStream.read(buffer)) > 0) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
Note that I've left it for the callerto clean up the input stream. Typically whoever fetches the stream also closes it - it would be slightly odd for a method like this to close it itself, IMO. You could do so if you wanted to, but probably in a finally
block so you'd know that it would alwaysbe closed even if an exception was thrown.
请注意,我已将其留给调用者清理输入流。通常,获取流的人也会关闭它 - 像这样的方法自行关闭它会有点奇怪,IMO。如果您愿意,您可以这样做,但可能在一个finally
块中,这样您就知道即使抛出异常,它也会始终关闭。
Note that your "writing" code later on won't close the file if there's an exception, either. You should get in the habit of always closing streams in finally
blocks.
请注意,如果出现异常,您稍后的“编写”代码也不会关闭文件。你应该养成总是在finally
块中关闭流的习惯。