Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
<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:
@Entity
public class SomeEntity {
private byte[] image;
// ...
As to dealing with Part
, you thus basically need to read its InputStream
into a byte[]
. You can use InputStream#readAllBytes()
for this:
InputStream input = uploadedFile.getInputStream();
byte[] image = input.readAllBytes();
someEntity.setImage(image);
// ...
entityManager.persist(someEntity);
Or if you're not on Java 9 yet, then head to Convert InputStream to byte array in Java for alternative ways to read an InputStream
into a byte[]
.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.