Image 在GWT中从我的sql检索图像
我正在研究GWT RPC。我在从SQL检索图像时遇到问题。 这是我的密码:Image 在GWT中从我的sql检索图像,image,gwt,Image,Gwt,我正在研究GWT RPC。我在从SQL检索图像时遇到问题。 这是我的密码: Base64 bas = new Base64(); // sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); UploadfileJpaController up = new UploadfileJpaController(); // this function returns the value in blob field in the
Base64 bas = new Base64();
// sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
UploadfileJpaController up = new UploadfileJpaController();
// this function returns the value in blob field in the form of byte array
byte[] b = up.findUploadfile(n);
String base64Contents = enc.encode(b).replaceAll("\\s+", "");
//String base64 = Base64Utils.toBase64(b);
base64Contents = "data:image/gif;base64,"+base64Contents;
return base64Contents;
但是这不起作用。。图像不显示。请提供帮助:(您应该让常规servlet负责返回图像数据,而不是使用GWT-RPC。servlet应该设置正确的图像/gif头并将二进制数据写入响应输出流。 编辑 这应该看起来有点像这样 公共类FileDownloadServlet扩展了HttpServletv{
// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Set content type
resp.setContentType("image/gif");
//Up to you!
byte[] binaryData = getDataFromDbase();
ByteArrayInputStream bis = new ByteArrayInputStream(binaryData);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = bis.read(buf)) >= 0) {
out.write(buf, 0, count);
}
bis.close();
out.close();
}
}
您的url将类似于
http://server/application/image_servlet?id=123545
在哪里可以使用servlet中的id参数来查找图像。当然,还可以将servlet添加到web.xml中。祝您好运。尝试显示图像的客户端代码在哪里?