在我的 Android 应用程序中,我尝试使用以下 URL 从 Windows Azure Blob 存储下载:http://iclyps.blob.core.windows.net/broadcasts/23_6.mp4
当我从应用程序中下载生成的文件时,该文件已损坏。当我使用默认浏览器或 Chrome 下载它时,会出现同样的错误。同样在 Easy Downloader 应用程序中,也会出现相同的错误。仅从我的 PC 下载或从 Android 设备(或模拟器)使用 Firefox Beta,才能正确检索文件。
我使用以下代码( fragment ):
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
bis = new BufferedInputStream(urlConnection.getInputStream(), BUFSIZE);
bos = new BufferedOutputStream(
context.openFileOutput(TMPFILE, Context.MODE_PRIVATE), BUFSIZE);
/*
* Read bytes to the buffer in chunks of BUFSIZE bytes until there is nothing more to read.
* Each chunk is written to the output file.
*/
byte[] buf = new byte[BUFSIZE];
int nBytes = 0;
int tBytes = 0;
while ((nBytes = bis.read(buf, 0, BUFSIZE)) > 0) {
bos.write(buf, 0, nBytes);
tBytes += nBytes;
}
if (tBytes == 0) throw new Exception("no bytes received");
bos.flush();
MobyLog.d(TAG, "download succeeded: #bytes = " + Integer.toString(tBytes));
return true;
} catch (Exception e) {
MobyLog.e(TAG, "download failed: " + e);
context.deleteFile(TMPFILE); // remove possibly present partial file.
return false;
} finally {
if (bis != null) try { bis.close(); } catch (IOException e) {MobyLog.e(TAG, "bis close exception: " + e); };
if (bos != null) try { bos.close(); } catch (IOException e) {MobyLog.e(TAG, "bos close exception: " + e); };
}
分析文件表明,原始文件的第一部分(约 700K)在损坏的文件中重复了多次,导致 mp4 文件无效。
将文件放在另一个网络服务器 (Apache/IIS) 上,然后从该位置下载文件确实会导致正确的下载。
有人在从 Azure 执行下载时遇到过类似的问题吗?有人可以提供解决方案吗?
干杯, 哈拉尔德...
最佳答案
您是否尝试过使用 azure-sdk-for-java在你的 Android 应用程序中?
我们的场景略有不同,因为我们使用 sdk 将图像从 blob 存储拉取和推送到自定义 Android 应用程序。但基本原理应该是相同的。
关于Android 从 Azure Blob 存储下载导致文件无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12497156/