【发布时间】:2015-04-02 12:39:45
【问题描述】:
我的 bean 中有一个 byte[] 的图像。用户可以使用<h:inputFile/> 上传图片。用户点击上传后,图像应立即显示在页面中(不应保存在数据库/文件夹中)。我尝试使用 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/> 但没有帮助。请帮我解决它。
【问题讨论】:
-
您为什么不将它保存在某个地方?当然这就是他们上传它的目的。
-
是的,正确。在点击 Save 时还有另一个 Save 按钮,它将被保存到 DB。保存工作正常,唯一的问题是显示。
-
所以立即将它保存到数据库中,但是带有一个标志来表示它是未确认的——或者将它保存在不同的表中,也许。除此之外,这意味着当用户确实单击保存时,您已经获得了数据...并且这意味着您可以简单地显示它。
-
但问题是如果用户关闭浏览器图像将被保存在不应该保存的数据库中。除了图像之外,我们还有许多用户将输入数据并保存的字段。如果不想保存数据,那么我们需要丢弃所有内容。
-
当然,因此您可以有效地进行垃圾收集 - 在用户首次上传图像时保留时间戳,然后在一小时或类似的时间后将其清除。在他们首次上传数据时保留数据将使您的生活整体很多更简单。