Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Enforcing Upload Limits with Primefaces 2.2.1

DZone's Guide to

Enforcing Upload Limits with Primefaces 2.2.1

· Java Zone ·
Free Resource

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend.

A very short tip I came across lately. If you are looking into enforcing some limits to your upload component:

<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
description="Allowed Files"/> 

you are most likely able to use allowTypes and sizeLimit attributes. But what about enforcing project wide settings without repeating this in any faces template over and over again?
Simple as you might guess: Write your own component. All you have to do is to extend the org.primefaces.component.fileupload.FileUpload component a bit like this:

public class ExcludeListPrimeFileUpload extends FileUpload {
public ExcludeListPrimeFileUpload() {
super();

// allowed file-types
setAllowTypes("*.doc;*.docx;*.pdf;*.ppt;*.pptx;*.zip;*.txt");
//size limit 30 megabyte = 31 457 280 bytes
setSizeLimit(new Long(31457280));
}
}

and register it as a replacement for the org.primefaces.component.FileUpload component-type with your faces-config.xml.

<component-type>org.primefaces.component.FileUpload</component-type>
<component-class>net.eisele.test.primetest.ExcludeListPrimeFileUpload</component-class>
</component>

If you use the Browsee Button you now see, that you are only able to select any of the provided file-types:

That was simple and quick. Thanks PrimeFaces ;)

 

From http://blog.eisele.net/2011/05/enforcing-upload-limits-with-primefaces.html

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend. 

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}