java GWT - 来自数据库的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4553316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GWT - image from database
提问by Hons
I'm actually working on a GWT based website. Now I'm stuck on how I should display images stored in a database on my website.
我实际上在一个基于 GWT 的网站上工作。现在我被困在如何在我的网站上显示存储在数据库中的图像。
Basically I've a bytearray in my database, which I fetch using hibernate. Now should I probably create an ... tag out of that data, but I don't know how
基本上我的数据库中有一个字节数组,我使用休眠来获取它。现在我应该从这些数据中创建一个 ... 标签,但我不知道如何
I'm using GWT in Java and Hibernate
我在 Java 和 Hibernate 中使用 GWT
采纳答案by Gursel Koca
Here is the solution. First you should encode the byte array by using com.google.gwt.user.server.Base64Utils.toBase64(byte[]) . But this method does not work for IE 7. and IE8 has 32kb limit.. IE9 does not have this limit.
这是解决方案。首先,您应该使用 com.google.gwt.user.server.Base64Utils.toBase64(byte[]) 对字节数组进行编码。但是这个方法对IE 7 不起作用。IE8 有32kb 的限制。IE9 没有这个限制。
here is the method on the server
这是服务器上的方法
public String getImageData(){
String base64 = Base64Utils.toBase64(imageByteArray);
base64 = "data:image/png;base64,"+base64;
return base64;
}
Here is the client method ;
这是客户端方法;
@Override
public void onSuccess(String imageData) {
Image image = new Image(imageData);
RootPanel.get("image").add(image);
}
回答by ch4nd4n
I don't know how GWT works, albeit you can map a servlet/controller which returns resourceStream. For example if you map a servlet "imageViewer" which takes imageId param, request to image would become
我不知道 GWT 是如何工作的,尽管您可以映射一个返回 resourceStream 的 servlet/控制器。例如,如果您映射一个带有 imageId 参数的 servlet“imageViewer”,则对图像的请求将变为
/imageViewer?imageId=1234
Hibernate object would have reference to the blob, so you can return that. Reference on UI would be
Hibernate 对象将引用 blob,因此您可以返回它。UI 上的参考将是
<img src="/imageViewer?imageId=1234"/>
Update: You may not be able to use Model as it is to return image, you would need an explicit controller or servlet which returns stream data.
In servlet you would do something like
更新:您可能无法使用 Model 来返回图像,您需要一个显式控制器或 servlet 来返回流数据。
在 servlet 中你会做类似的事情
// get reference to input stream
InputStream in = hibnerateObject.getImage();
// set MIME type etc
response.setContentType(mimeType);
OutputStream out = response.getOutputStream();
while ((len = in.read(buf)) >= 0)
out.write(buf, 0, len);
in.close();
out.close();
回答by EugeneD
I used the same approach as Gursel Koca suggested but could only get it to work using the Apache Base64 library, not (ironically) the GWT Base64Utils
我使用了与 Gursel Koca 建议的相同的方法,但只能使用 Apache Base64 库,而不是(具有讽刺意味的)GWT Base64Utils
String base64 = Base64.encodeBase64String(array);
base64 = "data:image/"+type+";base64," + base64;
return base64;
Also note that if you are updating an existing image or an image placeholder, the setURL method will overwrite your stylesheet, so make sure to grab that first:
另请注意,如果您要更新现有图像或图像占位符,setURL 方法将覆盖您的样式表,因此请确保先获取它:
String styleName = profilePicture.getStyleName();
profilePicture.setUrl(base64String);
profilePicture.setStyleName(styleName);
回答by EugeneD
There is Image Widgetin GWT. You can't do it client-side but you can call RPCto communicate with the server. Then it is simple CRUD application. In server connect to database with hibernate and return the Image to the client or it's url and on the client-side do something like that :
GWT 中有Image Widget。您不能在客户端执行此操作,但可以调用RPC与服务器进行通信。然后是简单的 CRUD 应用程序。在服务器中使用休眠连接到数据库并将图像返回给客户端或它的 url 并在客户端执行类似的操作:
@Override
public void onSuccess(String imageUrl) {
Image image = new Image(imageUrl);
RootPanel.get("image").add(image);
}
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getMessage());
}
That's all. Happy coding
就这样。快乐编码