Primefaces fileUpload无法使用文件大小> 1MB。

时间:2014-03-11 15:04:13

标签: file-upload primefaces jsf-2.2 apache-commons-fileupload

我上传大于1mb的文件有问题,没有调用FileUploadEvent,因为较小的文件正常工作。

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);
        }
    }
}

网页:

<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>

我正在使用; 公地文件上传-1.2.2 公地IO-2.3 primefaces-4.0 jsf 2.2

2 个答案:

答案 0 :(得分:1)

2年后,我会给你答案......哈哈 更改Web服务器中的“最大发布大小”。 如果您使用Glassfish: 配置 - &gt;网络配置 - &gt; listner - &gt; http - &gt; “最大邮政大小”

这个问题困扰了我3个月。

答案 1 :(得分:0)

上传的文件保存在D驱动器中。它有效。

使用以下代码更新handleFileUpload方法

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();
    }
}