PrimeFaces: fileUpload to byte []

I am trying to upload the downloaded image <p:fileUpload> as byte[] and save it to DB by JPA. But I ran into a problem, and I'm not even sure if I code it correctly.

This is the object:

 @Entity class Object { @Lob @Column(name = "image") private byte[] image; //... } 

Managed bean:

 @ManagedBean class MyBean { private Object ob = new Object(); @EJB private ObjectFacadeLocal of; public void handleFileUpload(FileUploadEvent event) { byte[] content = event.getFile().getContents(); ob.setImage(content); } public String submit() { //... of.create(ob); return "anotherpage" } 

ObjectFacade stores Object in the database.

This is a JSF page called form.xhtml :

 <h:form id = "upi" enctype = "multipart/form-data"> <p:fileUpload fileUploadListener="#{MyBean.handleFileUpload}" allowTypes="*.jpg;*.png;*.gif;" description="Images"/> <h:commandButton value = "Submit" action = "#{MyBean.create()}"> </h:commandButton> </h:form> 

First of all, I get a warning in form.xhtml that it cannot find the handleFileUpload method, but I can still run it. When I click the "Submit" button, nothing happens, the page just refreshes. If I remove the enctype attribute, then the object will be saved, but not with the image.

Any ideas?


Update :

This is my web.xml :

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /web-app_3_0.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/blog.xhtml</welcome-file> </welcome-file-list> <filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> </filter> <filter-mapping> <filter-name>PrimeFaces FileUpload Filter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> </web-app> 
+4
source share
1 answer

You forgot to read the PrimeFaces User Guide . Here's the excerpt <p:fileUpload> chapter:

Getting started with FileUpload

The first thing to do is set up a file upload filter that analyzes multiple requests. The FileUpload filter should appear in the Faces Servlet.

 <filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> </filter> <filter-mapping> <filter-name>PrimeFaces FileUpload Filter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> 

Without this filter, no action method can be called inside <h:form enctype="multipart/form-data"> , and the submitted data will not be correctly analyzed.

As for the warning that the method cannot be found, this is due to the IDE you are using, which pretends to be smarter than it is. Disable the EL check in this IDE to avoid these confusing warnings and see if you can update the IDE.

+4
source

Source: https://habr.com/ru/post/1386623/


All Articles