0
votes

I am using JSF 2.2 framework using primefaces, but I am unable to create multiple files import .I have tried using the lots of option but it doesn't work

public static Collection getAllParts(Part part) throws ServletException, IOException {

    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
   
    return request.getParts().stream().filter(p -> part.getName().equals(p.getName())).collect(Collectors.toList());
}

public void submit() throws ServletException, IOException {

for (Part part : getAllParts(file)) {

    String fileName = part.getSubmittedFileName();

    InputStream fileContent = part.getInputStream();
    // ... 
    //
    // E.g. https://stackoverflow.com/q/14211843/157882
}

}

But it is still not working.Moreover HttpServletRequest request is returning null value. Please help me in resolving this issue

1
Hi, Liferay is a Portlet based JSF platform and not a Servlet based one. So anything from javax.servlet.* API is not available. That's your #1 mistake. You need the javax.portlet.* ones instead, such as PortletRequest. Have you already tried Googling around with keywords like "Multiple file upload using Liferay 7" instead of "Multiple file upload using JSF"? When I try so then I see relatively a lot of apparently useable examples.BalusC

1 Answers

0
votes

Use jsf and Primefaces instead of the servlet request.

e.g.

In your web.xml

<context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>auto|native|commons</param-value>
</context-param>
<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>

In your pom

<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.4</version>
</dependency>

In your .xhtml :

<h:form id="uploadingForm" enctype="multipart/form-data">
        <p:fileUpload listener="#{yourbean.handleFileUpload}" 
                        mode="advanced" dragDropSupport="false"
                        multiple="true" update="messages" sizeLimit="100000" fileLimit="3" /> 
</h:form>

In your bean:

   private List<UploadedFile> uploadedFiles;

    public void handleFileUpload(FileUploadEvent event) {
             uploadedFiles.add(event.getFile());
    }

now can you process your files on the uploadedFiles list

you can even check the documentation