1

我使用的代码的问题是该文件出现在 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) {
    }
4

1 回答 1

3

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

is.position = 0;
blob.upload(is, fileReference.length());
于 2013-10-09T21:48:42.280 回答