我正在研究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 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;
但这不起作用..图像不显示。请帮忙:(
答案 0 :(得分:0)
您应该让常规servlet负责返回图像数据,而不是使用GWT-RPC。 servlet应设置正确的image / 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();
}
}
你的网址会像
http://server/application/image_servlet?id=123545
您在servlet中使用id参数来查找图像。当然,将servlet添加到web.xml中。祝你好运。