Mentawai Web Framework

File Upload

Mentawai is integrated with Apache Commons FileUpload so you can easily handle file upload through the org.menatawai.filter.FileUploadFilter. Just notice that you must place the commons-fileupload-1.0.jar inside your web applicatoin's /WEB-INF/lib directory.

Check the example bellow:

public class HelloFileUpload extends BaseAction {
    public String execute() throws Exception {
        String username = input.getStringValue("username");
        if (username == null || username.trim().equals("")) {
            return ERROR;
        output.setValue("username", username.toUpperCase());
        // get a ready-to-use org.apache.commons.fileupload.FileItem
        FileItem item = (FileItem) input.getValue("theFile");
        output.setValue("fileSize", String.valueOf(item.getSize()));
        String [] options = input.getStringValues("options");
        output.setValue("options", options);
        return SUCCESS;

And for the filter configuration:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
    public void loadActions() {
        ActionConfig ac = new ActionConfig("/HelloFileUpload", HelloFileUpload.class);
        ac.addConsequence(HelloFileUpload.SUCCESS, new Forward("/hello.jsp"));
        ac.addConsequence(HelloFileUpload.ERROR, new Forward("/username.jsp"));
        ac.addFilter(new FileUploadFilter());

And for the HTML form, it is just the regular stuff:

<h1>Hello Metawai!</h1>
<form action="HelloFileUpload.mtw" method="post" enctype="multipart/form-data"><br>
Enter an username: <input name="username" size="25" /><br>
Enter filename: <input name="theFile" type="file" /><br>
<input name="options" type="checkbox" value="1"> Option 1 <input name="options" type="checkbox" value="2"> Option 2 <br>
<input type="submit" value="Enviar">

For more information on how to use the FileItem, please refer to the Apache Commons FileUpload API documentation.

You may also click here to download an FileUpload example.