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