2012-04-03 36 views
0

我正在使用GWT RPC。我在從SQL中檢索圖像時遇到問題。 這裏是我的代碼:在GWT中從我的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; 

但這不是工作..不顯示圖像。請幫忙:(

+1

其中是試圖顯示圖像的客戶端代碼? – Dojo 2012-04-03 13:34:35

回答

0

你應該讓一個普通的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中。祝你好運。

+0

你可以請指導我如何使用,我是一個完整的初學者.. – 2012-04-04 05:31:26