记录一下Blodstore Java API的学习过程 官方doc地址
这里详细解释一下它这里上传文件的机制
下面是官网的描述使用Blobstore的过程:
Applications can use the Blobstore to accept large files as uploads from users and to serve those files. Files are called blobs once they're uploaded. Applications don't access blobs directly. Instead, applications work with blobs throughblob info entities (represented by the BlobInfo
class) in the datastore.
The user creates a blob by submitting an HTML form that includes one or more file input fields. Your application setsblobstoreService.createUploadUrl()
as the destination (action) of this form, passing the function a URL path of a handler in your application. When the user submits the form, the user's browser uploads the specified files directly to the Blobstore. The Blobstore rewrites the user's request and stores the uploaded file data, replacing the uploaded file data with one or more corresponding blob keys, then passes the rewritten request to the handler at the URL path you provided toblobstoreService.createUploadUrl()
. This handler can do additional processing based on the blob key.
The application can read portions of a Blobstore value using a file-like streaming interface. See theBlobstoreInputStream
class.
结合它给的demo可以这样来理解
- 在需要上传文件的页面中将文件提交给
来处理其中它的参数是用来指定上传结束后处理后续操作的handler的url地址。下面是createUploadUrl(...)方法的说明:createUploadUrl("/upload")
String com.google.appengine.api.blobstore.BlobstoreService.createUploadUrl(String successPath)
Create an absolute URL that can be used by a user to asynchronously upload a large blob. Upon
completion of the upload, a callback is made to the specified URL.
Parameters:
successPath A relative URL which will be invoked after the user successfully uploads a blob.
Must start with a "/".
下面是一个要上传文件的表单:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Upload Test</title>
</head>
<body>
<form action=<%=blobstoreService.createUploadUrl("/upload") %>
method="post" enctype="multipart/form-data">
<input type="text" name="foo">
<input type="file" name="myFile">
<input type="submit" value="Submit">
</form>
...
2.因此当用户点击了submit按钮之后文件已经在上传了,在这个过程中对我们而言是透明的 ,但是我们需要观察到上传的结果这个时候官网的doc里面说“When the user submits the form, the user's browser uploads the specified files directly to the Blobstore. The Blobstore rewrites the user's request and stores the uploaded file data, replacing the uploaded file data with one or more corresponding blob keys, then passes the rewritten request to the handler at the URL path you provided toblobstoreService.createUploadUrl()
. This handler can do additional processing based on the blob key.” 上传完成之后Blobstore 重写了我们的request那么如果我们需要对这个上传操作之后要进行后续操作的话,只需要根据这个重写后的request去提取我们上传的文件的信息,于是乎我们何以在我们的handler (这里就是servlet)中进行如下的处理来获取已经上传的文件的信息。
private BlobstoreService blobstoreService=BlobstoreServiceFactory.getBlobstoreService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
@SuppressWarnings("deprecation")
Map<String,BlobKey> blobs=blobstoreService.getUploadedBlobs(req);
BlobKey blobKey=blobs.get("myFile");
通过将request对象传入getUploadBlobs方法中我们就能够获取到第一步的上传请求的信息(注意这个request是经过重写的,包括handler的url(即“/upload”)),然后我们就能够遍历整个目录下的所有blobs,来验证我们的文件是否真正的上传到Blobstore中去了,或者进行一些其他的逻辑处理(比如:跳转到提示页面去)。
可以再这里下载demo