我的bean中有一个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代码是
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 :(得分:2)
Finall我能够在IE中显示我的图像。我的要求是以275px * 75px显示图像。对于此要求,我使用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;
}
希望这可以帮助那些试图在网页中显示小尺寸大图像的人。