How to convert Part to Blob, so I can store it in MySQL? It is an image. Thank you
My form
<h:form id="form" enctype="multipart/form-data">
<h:messages/>
<h:panelGrid columns="2">
<h:outputText value="File:"/>
<h:inputFile id="file" value="#{uploadPage.uploadedFile}"/>
</h:panelGrid>
<br/><br/>
<h:commandButton value="Upload File" action="#{uploadPage.uploadFile}"/>
</h:form>
My bean
@Named
@ViewScoped
public class UploadPage {
private Part uploadedFile;
public void uploadFile(){
}
}
The SQL database BLOB type is in Java represented as
byte[]
. This is in JPA further to be annotated as@Lob
. So, your model basically need to look like this:As to dealing with
Part
, you thus basically need to read itsInputStream
into abyte[]
. Apache Commons IOIOUtils
is helpful here:Or if you prefer standard Java API which is only a bit more verbose: