2014-02-26 33 views
0

上是我的代碼在GWT項目如何以檢索其存儲爲BLOB的圖像,並顯示在頭版下方

以及如何從數據存儲和顯示在頭版retrive的形象,這是一個GWT項目,圖像被存儲爲Blob,如何將其傳輸到圖像?任何人都可以給我一個關於圖像存儲和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; 

回答

0

你必須創建一個叫 「服務」 上blobstoreService功能的servlet。

檢查從谷歌的例子:here

編輯:

你必須瞭解的BlobstoreService工作流程

This tutorial會幫助你。

我在您的代碼中看到您將用戶圖像保存爲Blob。在模型中使用url更好。 (它更輕巧)

總結:

客戶端: 你有從調用服務的設置的操作字段上傳的URL FormWidget blobServiceInstance.createUploadUrl("/your_upload_Servlet");
(不要忘了設置加密類型參數的形式 「的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); 
    } 

所以,當你保存用戶與保存這樣的圖片網址:

「?/服務的blob鍵=」 ag5ncmVlbi1hbmFseXplcnITCxIGUHJvamV0GICAgICAgMAIDA「」

您將使用此URL在客戶端獲取圖像。

希望它有幫助。

PS:對不起,我的英語:)

+0

你能給我你的代碼嗎?我已經看到了這個鏈接的內容,但我還不明白。我想要使​​用GWT的FileUpload小部件,上傳一張圖片,然後我可以檢索圖片並將其顯示在首頁上。 – jeff

相關問題