2015-04-02 32 views
0

工作,我有一個形象在我的豆byte[]。用戶可以使用<h:inputFile/>上傳圖片。只要用戶點擊上傳,圖像應該顯示在頁面中(不應保存在DB /文件夾中)。我嘗試使用Base64轉換,但在IE中它只顯示32KB大小的圖像。 以下是我的xhtml代碼。
<h:form id="signatureform" enctype="multipart/form-data"> <h:inputFile id="inputFile" label="file1" value="#{bean.part}"/> <h:commandButton id="upButton" type="submit" action="#{bean.uploadSignature}" value="Upload" styleClass="commandButton"> </h:commandButton> </h:form>
<h:graphicImage url="data:image/jpg;base64,#{bean.encodedImage}" width="275px" height="75px"></h:graphicImage>
我的Java代碼爲
將字節數組顯示爲圖像; BASE64數據URI不32KB以上在IE

private String encodedImage ; 
private Part part; 
//getters and setters 
public String uploadSignature() throws IOException { 

    InputStream inputStream = null; 
    try { 
     inputStream = part.getInputStream(); 
     encodedImage = new String(Base64.encode(IOUtils.toByteArray(inputStream))); 
    } catch (Exception e) { 

    } finally { 
     if (inputStream != null) { 
      inputStream.close(); 
     } 
    } 
    return SUCCESS; 
} 


我與<a4j:mediaOutput/>但沒有幫助嘗試。請幫我解決它。

+0

爲什麼你會*不*在某處被保存呢?當然,這是他們上傳的重點。 – 2015-04-02 12:42:24

+0

是的。保存按鈕將保存到數據庫中。保存工作正常,只顯示問題。 – 2015-04-02 12:45:02

+0

因此,將它立即保存到數據庫中*,但用一個標誌來說明它未經確認 - 或將其保存在不同的表格中,或許。除此之外,這意味着當用戶*單擊保存時,您已經獲得了數據... *和*這意味着您可以輕鬆地顯示它。 – 2015-04-02 12:46:42

回答

1

Finall我能在IE中顯示我的形象。我的要求是顯示275像素* 75像素的圖像。爲了這個要求,我調整了我的圖像在Java中的大小,並顯示在網頁中。當我調整圖像大小並轉換爲Base64字符串時,它變得小於32kb。我嘗試了12MB的圖像,它工作得很好:)。以下是我的代碼。

 BufferedImage imBuff = ImageIO.read(inputStream); 
     BufferedImage resizedImg = resize(imBuff, 275, 75); 
     ByteArrayOutputStream os = new ByteArrayOutputStream(); 
     ImageIO.write(resizedImg, "jpg", os); 
     encodedImage = new String(Base64.encode(os.toByteArray())); 



private BufferedImage resize(BufferedImage image, int newWidth, int newHeight) { 
     int currentWidth = image.getWidth(); 
     int currentHeight = image.getHeight(); 
     BufferedImage newImage = new BufferedImage(newWidth, newHeight, image.getType()); 
     Graphics2D graphics2d = newImage.createGraphics(); 
     graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
       RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
     graphics2d.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, 
       currentWidth, currentHeight, null); 
     graphics2d.dispose(); 
     return newImage; 
    } 

希望這將幫助其他人誰試圖在網頁中顯示的小尺寸大的圖像。