image upload using primefaces

Posted: August 27, 2012 in web

add xhtml

<p:fileUpload id=”tmp-image” value=”#{bean.uploadedFile}”
mode=”simple” sizeLimit=”100000″
allowTypes=”/(\.|\/)(gif|jpe?g|png)$/” />


private UploadedFile uploadedFile;

public String saveAd() {

try {
Card card = new Card();

public StreamedContent getImage() {
if (facesContext.getRenderResponse()) {
return new DefaultStreamedContent();
ByteArrayInputStream imageStream = null;
for (Card card : cards) {
int id = Integer.parseInt(facesContext.getExternalContext().getRequestParameterMap().get(“id”));
if (card.getId() == id) {
imageStream = new ByteArrayInputStream(card.getImage());
return new DefaultStreamedContent(imageStream);

display xhtml

<p:graphicImage value=”#{bean.getImage}”>
<f:param name=”id” value=”#{}” />


@Column(name = “Image”)
private byte[] image;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s