实现功能:
1. 通过Web界面上传本地图片文件到GAE的Blobstore;
2. 通过GAE自动生成的URL访问图片;
3. 手动提供图片访问服务;
代码:
上传页面(hello.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>
<%
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
%>
<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>
<body>
<h1>ImageUpload</h1>
<h2>New</h2>
<form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Submit">
</form>
</body>
</html>
Upload.java
package gaeimage;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
public class Upload extends HttpServlet {
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("myFile");
if (blobKey == null) {
res.sendRedirect("/");
} else {
res.sendRedirect("/server?blob-key=" + blobKey.getKeyString());
System.out.println(blobKey.getKeyString());
}
}
}
Server.java
package gaeimage;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;
public class Server extends HttpServlet {
BlobKey blobKey;
private ImagesService imagesService = ImagesServiceFactory.getImagesService();
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
String type=req.getParameter("type");
if (type == null)
{
blobKey = new BlobKey(req.getParameter("blob-key"));
String imageUrl = imagesService.getServingUrl(blobKey);
System.out.println(imageUrl);
}
else if (type.equalsIgnoreCase("showImage"))
{
BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
byte[] imageData = blobstoreService.fetchData(blobKey, 0, blobInfo.getSize());
res.setContentType("image/jpeg");
res.getOutputStream().write(imageData);
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Servlets -->
<servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>gaeimage.Upload</servlet-class>
</servlet>
<servlet>
<servlet-name>Server</servlet-name>
<servlet-class>gaeimage.Server</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Server</servlet-name>
<url-pattern>/server</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value/>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
</web-app>
与想象中的不同,访问存储在Blobstor中图像数据时,不能通过如下方法直接获取数据
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image image = ImagesServiceFactory.makeImageFromBlob(blobKey);
byte[] imageData = image.getImageData();
这样获取到的数据为空,非常奇怪的一个问题。
最终,我们可以通过web页面上传图像之后,通过:your_url/type=showImage 来访问上传的图像。