我有一个发布的Android应用程序,该应用程序具有HTTP音频下载过程。
到今天为止,此过程运行良好。
我的代码有什么问题?
final URL downloadFileUrl = new URL(mPerformanceSong.getPreviewUrl());
final HttpURLConnection httpURLConnection = (HttpURLConnection) downloadFileUrl.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.connect();
mTrackDownloadFile = new File(RecordPerformance.this.getCacheDir(), "mediafile");
mTrackDownloadFile.createNewFile();
final FileOutputStream fileOutputStream = new FileOutputStream(mTrackDownloadFile);
final byte buffer[] = new byte[16 * 1024];
final InputStream inputStream = httpURLConnection.getInputStream();
int len1 = 0;
while ((len1 = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len1);
}
fileOutputStream.flush();
fileOutputStream.close();
下载文件的内容似乎是gzip。
这是否意味着我需要将我的inputStream包装在GZIPInputStream中?
一个示例下载URL是
http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI
最佳答案
我真的很惊讶它下载了什么-您确定是吗?
您发布的http网址:
http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI
当前正在重定向到https cloudfront地址:
https://d28julafmv4ekl.cloudfront.net/...
HttpUrlConnection
将不遵循跨方案的重定向(即从http到https)。因此,如果将* .amazon.com URL更改为https,也许可以解决您的问题...
关于android - 如何在Android中下载音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26066266/