在gwt项目中如何检索存储为blob的图像并将其显示在首页上

时间:2014-02-26 00:31:32

标签: image gwt blob

下面是我的代码

以及如何从数据存储区中检索图像并显示在首页上,这是一个GWT项目,图像存储为Blob,如何将其传输到Image?任何人都可以给我一个关于图像存储和在GWT项目中显示的例子。

enter code here我的代码:

//the User class which the image stored as blob

package cn.hjf.client;

import java.util.Date;   
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Blob;
import com.google.gwt.user.client.rpc.IsSerializable;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements IsSerializable{

@Persistent
private String userName;//用户名

@Persistent
Blob userImage;


@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long userId;

public User(){}

public User(String name,Blob image){
    this.userName = name;
    this.userImage = image;

}

public Blob getUserImage() {
    return userImage;
}

public void setUserImage(Blob userImage) {
    this.userImage = userImage;
}

public Long getUserId() {
    return userId;
}

public void setUserId(Long userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

}

//the servlet which store image 

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
// TODO Auto-generated method stub
ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iter;
try {
    iter = upload.getItemIterator(req);
    FileItemStream imageItem = iter.next();
InputStream imgStream = imageItem.openStream();
// construct our entity objects
    Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
        // User user = new User(imageItem.getName(), imageBlob);
    User user = new User("xxx", imageBlob);

    // persist image
    PersistenceManager pm = Persister.get().getPersistenceManager();
    pm.makePersistent(user);
    pm.close();

} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


// respond to query
res.setContentType("text/plain");
res.getOutputStream().write("OK!".getBytes());

//this code segment can retrive the image as Blob

Blob imageFor(String name, HttpServletResponse res) {
// find desired image
 Image image = new Image();
PersistenceManager pm = Persister.get().getPersistenceManager();
Query query = pm.newQuery(User.class);          
List<User> results = (List<User>)query.execute();
image = results.iterator().next().getUserImage();
// serve the first image
res.setContentType("image/jpeg");
try {
    res.getOutputStream().write(image.getBytes());
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

System.out.println(image.getBytes());
return image;

1 个答案:

答案 0 :(得分:0)

您必须创建一个在blobstoreService上调用“serve”函数的Servlet。

检查来自google的示例:here

修改

您必须了解 BlobstoreService工作流程

This tutorial会帮助你。

我在您的代码中看到您将用户图像保存为Blob。最好在模型中使用url。 (它更轻量级)

总结:

客户端: 您有一个FormWidget,它使用来自调用blobServiceInstance.createUploadUrl("/your_upload_Servlet");的服务的上传URL设置操作字段 (不要忘记在表单上将enctype参数设置为“multipart / form-data”

服务器端: 你有一个像这样的UploadServlet:

 public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {

        //this is the value in your input file form
        String uploadType = req.getParameter("uploadType");
        List<BlobKey> blobKeys = blobstoreService.getUploads(req).get(uploadType);
        System.out.println("Url of the file : "+ "/serve?blob-key=" + blobKeys.get(0).getkeyString());

你有另一个服务于图像的Servlet:

public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws IOException {
        BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
        blobstoreService.serve(blobKey, res);
    }

因此,当您保存一个用户保存的图像网址为:

“/服务?团块密钥=” ag5ncmVlbi1hbmFseXplcnITCxIGUHJvamV0GICAgICAgMAIDA “”

您将使用此网址在客户端获取图片。

希望它有所帮助。

PS:对不起我的英文:)