2011-01-29 81 views
33

我知道這可能已被問過10000次,但是,我似乎無法找到問題的直接答案。將字節[]轉換爲數據URI的Base64字符串

我有一個LOB存儲在我的數據庫中,代表一個圖像;我從數據庫中獲取該圖像,並且希望通過HTML IMG標記將其顯示在網頁上。這不是我首選的解決方案,但是,除非我能找到更好的解決方案,否則這是一個階段性的實施。

我試圖使用Apache共享編解碼器以下列方式爲字節[]轉換爲Base64:

String base64String = Base64.encodeBase64String({my byte[]}); 

然後,我想表明我的網頁這樣對我的形象:

<img src="data:image/jpg;base64,{base64String from above}"/> 

它顯示瀏覽器的默認「我找不到這個圖像」,圖像。

有沒有人有任何想法?

謝謝。

+0

你可以張貼的完整代碼...我需要它作爲參考。 ..我也有同樣的問題...但我沒有任何代碼將如何.. – Lucky 2013-01-15 13:33:05

回答

38

我用這個和它工作得很好(違背了公認的答案,它使用不推薦使用此方案的格式):

StringBuilder sb = new StringBuilder(); 
sb.append("data:image/png;base64,"); 
sb.append(StringUtils.newStringUtf8(Base64.encodeBase64(imageByteArray, false))); 
contourChart = sb.toString(); 
12

根據官方文檔Base64.encodeBase64URLSafeString(byte[] binaryData)應該是你在找什麼。

此外,JPG的MIME類型是image/jpeg

+1

[維基百科文章中的PNG例子](https://secure.wikimedia.org/wikipedia/en/ wiki/Data_URI_scheme#示例)表明使用+和/是可以的。 – 2011-01-29 18:07:05

+1

有沒有人真的試過使用Base64.encodeBase64URLSafeString()的輸出來查看它是否在瀏覽器中工作? encodeBase64URLSafeString()似乎不會生成有效/標準的base64編碼。 要使base64編碼的字符串符合URI,應該對其應用標準URI字符轉義。我不認爲上述功能是你想要的。 – Keeth 2013-08-14 17:28:11

+0

這個答案實際上並沒有爲我生成適當的字符串。 Hugo的答案包含缺失的編碼以使其正常工作。 – ryber 2013-12-12 22:30:03

2

您可能還想考慮將圖像流式傳輸到瀏覽器,而不是將其編碼到頁面本身。

這裏是通過servlet,它可以很容易地採用流式傳輸BLOB的內容,而不是文件輸出到瀏覽器的流媒體文件中包含的圖像的一個例子:

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
    throws ServletException, IOException 
    { 
    ServletOutputStream sos = resp.getOutputStream(); 
    try { 
     final String someImageName = req.getParameter(someKey); 

     // encode the image path and write the resulting path to the response 
     File imgFile = new File(someImageName); 

     writeResponse(resp, sos, imgFile); 
    } 
    catch (URISyntaxException e) { 
     throw new ServletException(e); 
    } 
    finally { 
     sos.close(); 
    } 
    } 

    private void writeResponse(HttpServletResponse resp, OutputStream out, File file) 
    throws URISyntaxException, FileNotFoundException, IOException 
    { 
    // Get the MIME type of the file 
    String mimeType = getServletContext().getMimeType(file.getAbsolutePath()); 
    if (mimeType == null) { 
     log.warn("Could not get MIME type of file: " + file.getAbsolutePath()); 
     resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
     return; 
    } 

    resp.setContentType(mimeType); 
    resp.setContentLength((int)file.length()); 

    writeToFile(out, file); 
    } 

    private void writeToFile(OutputStream out, File file) 
    throws FileNotFoundException, IOException 
    { 
    final int BUF_SIZE = 8192; 

    // write the contents of the file to the output stream 
    FileInputStream in = new FileInputStream(file); 
    try { 
     byte[] buf = new byte[BUF_SIZE]; 
     for (int count = 0; (count = in.read(buf)) >= 0;) { 
     out.write(buf, 0, count); 
     } 
    } 
    finally { 
     in.close(); 
    } 
    } 
1

如果您不想從servlet流式傳輸,然後將文件保存到webroot中的目錄中,然後創建指向該位置的src。這樣Web服務器就可以完成服務文件的工作。如果你感覺特別聰明,你可以通過timestamp/inode/crc32檢查現有文件,並且只有在數據庫中發生了變化才能提高性能的情況下將其寫出。此文件方法也會自動支持ETag和if-modified-since標題,以便瀏覽器可以正確緩存文件。

相關問題