【发布时间】:2014-08-16 22:52:57
【问题描述】:
我正在使用来自 android 应用程序的以下代码将 blob 上传到 Azure Blob 存储。注意:下面的sasUrl 参数是从我的网络服务获取的签名网址:
// upload file to azure blob storage
private static Boolean upload(String sasUrl, String filePath, String mimeType) {
try {
// Get the file data
File file = new File(filePath);
if (!file.exists()) {
return false;
}
String absoluteFilePath = file.getAbsolutePath();
FileInputStream fis = new FileInputStream(absoluteFilePath);
int bytesRead = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
while ((bytesRead = fis.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
fis.close();
byte[] bytes = bos.toByteArray();
// Post our image data (byte array) to the server
URL url = new URL(sasUrl.replace("\"", ""));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(15000);
urlConnection.setReadTimeout(15000);
urlConnection.setRequestMethod("PUT");
urlConnection.addRequestProperty("Content-Type", mimeType);
urlConnection.setRequestProperty("Content-Length", "" + bytes.length);
urlConnection.setRequestProperty("x-ms-blob-type", "BlockBlob");
// Write file data to server
DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
wr.write(bytes);
wr.flush();
wr.close();
int response = urlConnection.getResponseCode();
if (response == 201 && urlConnection.getResponseMessage().equals("Created")) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
代码对于小 blob 运行良好,但是当 blob 达到特定大小(取决于我正在测试的手机)时,我开始出现内存不足异常。我想拆分 blob 并将它们上传到块中。但是,我在网上找到的所有示例都是基于 C# 的,并且使用的是 Storage Client 库。我正在寻找一个 Java/Android 示例,该示例使用 Azure Storage Rest API 上传块中的 blob。
【问题讨论】:
标签: java android azure-blob-storage azure-storage