我已经通过java代码在Azure服务器上成功上传了图片,但是当我尝试下载图片时出现异常。
下载代码:
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.getContainerReference("mycontainer");
// Loop through each blob item in the container.
for (ListBlobItem blobItem : container.listBlobs()) {
// If the item is a blob, not a virtual directory.
if (blobItem instanceof CloudBlob) {
// Download the item and save it to a file with the same name.
CloudBlob blob = (CloudBlob) blobItem;
blob.download(new FileOutputStream("C:\\mydownloads\\" + blob.getName()));
}
}
}
获得以下异常,
java.util.NoSuchElementException: An error occurred while enumerating the result, check the original exception for details.
at com.microsoft.windowsazure.storage.core.LazySegmentedIterator.hasNext(LazySegmentedIterator.java:113)
at com.test.BlobBasics.main(BlobBasics.java:106)
Caused by: com.microsoft.windowsazure.storage.StorageException: The server encountered an unknown failure: OK
at com.microsoft.windowsazure.storage.StorageException.translateException(StorageException.java:187)
at com.microsoft.windowsazure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:251)
at
... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
at com.sun.xml.stream.XMLReaderImpl.next(XMLReaderImpl.java:563)
at com.microsoft.windowsazure.storage.blob.ListBlobsResponse.parseResponse(ListBlobsResponse.java:118)
at com.microsoft.windowsazure.storage.blob.CloudBlobContainer$6.postProcessResponse(CloudBlobContainer.java:1227)
at com.microsoft.windowsazure.storage.blob.CloudBlobContainer$6.postProcessResponse(CloudBlobContainer.java:1190)
at com.microsoft.windowsazure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:185)
... 2 more
答案 0 :(得分:0)
嘿使用下面的代码它会帮助你..
public void downloadAttachment() {
try {
final String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=your account name;" + "AccountKey=your account key";
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
// Get a reference to a container.
// The container name must be lower case
container = blobClient.getContainerReference("container name");
if (container.listBlobs() != null) {
Log.d("in if ", "list conatin value");
new downloadingAttachment().execute();
} else {
Log.d("in else ", "list does not any contain value ");
}
} catch (Exception e) {
// Output the stack trace.
e.printStackTrace();
}
}
public class downloadingAttachment extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Log.d("in if ", "container.listBlobs();" + container.listBlobs());
Iterable<ListBlobItem> valuesofConatainer = container.listBlobs();
Log.d("in if ", "valuesofConatainer=" + valuesofConatainer);
for (ListBlobItem blobItem : container.listBlobs()) {
// If the item is a blob, not a virtual directory.
if (blobItem instanceof CloudBlob) {
// Download the item and save it to a file with the same name.
CloudBlob blob = (CloudBlob) blobItem;
try {
blob.download(new FileOutputStream("your storage location" + blob.getName() + ".jpeg"));
Log.d(" blob.getName()=", " " + blob.getName());
break;
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StorageException e) {
e.printStackTrace();
}
}
}
return null;
}
}