0

I have a problem to upload files larger than 1mb the FileUploadEvent is not called, for smaller files works normally.

Web.xml:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>52428800</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>D:\</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

ManagedBean:

 @ManagedBean
@RequestScoped
public class TesteUpload {

    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        File file = new File(PropertiesLoader.getInstance().getPropertie(PropertiesLoader.PATCH_MIDIA), uploadedFile.getFileName());

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            byte[] buffer = new byte[6124];
            int bulk;
            InputStream inputStream = event.getFile().getInputstream();
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            FacesMessage error = new FacesMessage("Erro!!");
            FacesContext.getCurrentInstance().addMessage(null, error);
        }
    }
}

Page:

<ui:composition template="/public/templates/master.xhtml" 
            xmlns="http://www.w3.org/1999/xhtml" 
            xmlns:f="http://java.sun.com/jsf/core" 
            xmlns:h="http://java.sun.com/jsf/html" 
            xmlns:ui="http://java.sun.com/jsf/facelets" 
            xmlns:p="http://primefaces.org/ui">

<ui:define name="divMainPublic">

    <h:form enctype="multipart/form-data">
        <p:fileUpload fileUploadListener="#{testeUpload.handleFileUpload}" mode="advanced" sizeLimit="51380224" />
    </h:form>

</ui:define>

I am using; commons-fileupload-1.2.2 commons-io-2.3 primefaces-4.0 jsf 2.2

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
user3406652
  • 1
  • 1
  • 1

2 Answers2

2

I shall give you the answer, 2 years later... lol Change the "Max Post Size" in your web server. If you use Glassfish: Configurations - > network config -> listner -> http -> "Max Post Size"

This problem haunted me for 3 months.

0

Uploaded files are saved in D drive. It works.

Update handleFileUpload method with below code

public void handleFileUpload(FileUploadEvent event) {
    try {
        UploadedFile file = event.getFile();
        InputStream inputStream = file.getInputstream();
        OutputStream outputStream = new FileOutputStream("D:/"+ file.getFileName());
        byte[] bytes = file.getContents();
        int read = 0;
        while ((read = inputStream.read(bytes)) != -1 ) {
            outputStream.write(bytes, 0, read);
        }
        inputStream.close();
        outputStream.flush();
        outputStream.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92