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