How to insert uploaded image from p:fileUpload as BLOB in MySQL?
@Lob
@Column(name = "photo")
private byte[] photo;
And in XHTML page, I write this:
<p:inputText value="#{condidat.condidat.photo}" >
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
allowTypes="*.jpg;*.png;*.gif;" description="Images"/>
</p:inputText>
How can I retreive the value of uploaded file as byte[]?
You can get the uploaded file content via
FileUploadEvent. In PrimeFaces 4.x with Apache Commons FileUpload, or in PrimeFaces 5.x with context paramprimefaces.UPLOADERset tocommons, you can useUploadedFile#getContents()to obtain the uploaded file asbyte[].In PrimeFaces 5.x with context param
primefaces.UPLOADERabsent or set toautoornativewhile using JSF 2.2, thengetContents()will returnnullas that’s not implemented inNativeUploadedFileimplementation. UseUploadedFile#getInputStream()instead and then read bytes from it, e.g. with help of commons IO.Finally, just set this
byte[]in your entity and persist/merge it.Make sure that you have set the form encoding type to
multipart/form-dataand, when using the Apache Commons FileUpload, that you have configured the file upload filter inweb.xmlas per PrimeFaces user guide.