【问题标题】:How to upload an image to a blob in Azure?如何将图像上传到 Azure 中的 blob?
【发布时间】:2013-10-17 04:01:13
【问题描述】:

我使用的代码的问题是文件出现在 blob 中,但是作为 0B 大小的文件。好像没有数据写入 blob。

这是我在 JSP 中使用的形式:

<form method="POST" action="UploadServlet" enctype="multipart/form-data" >
    <input type="file" name="file" id="file" /> <br/>
    </br>
    <input type="submit" value="Upload" name="upload" id="upload" />
</form>

我的 servlet 中有以下代码:

    try {
                CloudStorageAccount account;
                CloudBlobClient serviceClient;
                CloudBlobContainer container;
                CloudBlockBlob blob;

                account = CloudStorageAccount.parse(storageConnectionString);
                serviceClient = account.createCloudBlobClient();
                container = serviceClient.getContainerReference("gettingstarted");
                container.createIfNotExist();

                // Set anonymous access on the container.
                BlobContainerPermissions containerPermissions;
                containerPermissions = new BlobContainerPermissions();
                                   containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
                container.uploadPermissions(containerPermissions);

                FileItemFactory factory = new DiskFileItemFactory();

            ServletFileUpload fileUpload = new ServletFileUpload(factory);

                //process only if its multipart content
                InputStream is = null;
                if (ServletFileUpload.isMultipartContent(request)) {
                    List items = fileUpload.parseRequest(request);
                    Iterator iter = items.iterator();
                    while (iter.hasNext()) {
                        FileItem item = (FileItem) iter.next();
                        if (!item.isFormField()) {
                            is = item.getInputStream();
                            String name = new File(item.getName()).getName();
                            System.out.println(name);
                            blob = container.getBlockBlobReference(name);
                            File fileReference = new File(item.getName());
                            System.out.println(is);
                            blob.upload(is, fileReference.length());
                        }
                    }
                }
                is.close();

                request.getRequestDispatcher("hello").forward(request, response);

    } catch (Exeption e) {
    }

【问题讨论】:

  • 这应该可行,你们与标准教程有多少偏差?
  • 我认为@Frans 的回答很好。您的输入流 is 未位于流的开头。在 C# 中,语法为:is.Seek(0, SeekOrigin.Begin); // Position the stream at the beginning,就在 blob.upload() 行之前。

标签: java file-upload azure azure-blob-storage apache-commons-fileupload


【解决方案1】:

我不知道 Java 中的等效语法,但在 C# 中,您通常必须将您在流中的位置重置回 0,然后再将其传递给 blob 上传方法(如果流已经阅读 - 它会让你得到长度)。 像

is.position = 0;
blob.upload(is, fileReference.length());

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2022-11-03
    • 2018-12-17
    • 2015-05-15
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多