File upload

Scooter framework provides easy APIs for file uploads. This document is based on the uploadexample under the examples/uploadexample directory.

File upload view

The file upload view is a regular HTML Form element with the multipart/form-data type. Developers can upload multiple files in the same request.

The name attribute in the input tag is used in the file upload controller class to retrieve the file.

<form name="filesForm" action="/uploadexample/files/upload" method="post" enctype="multipart/form-data">
    File 1:<input type="file" name="file1"/><br/>
    File 2:<input type="file" name="file2"/><br/>
    File 3:<input type="file" name="file3"/><br/>
    <input type="submit" name="Submit" value="Upload Files"/>

In this example, the upload request is going to be handled by the controller FilesController's upload() action method.

File upload controller class

Like all controller classes in Scooter framework, FilesController class uses static import to access many helper APIs provided by the ActionControl class.

import static com.scooterframework.web.controller.ActionControl.*;
import com.scooterframework.web.controller.UploadFile;

 * FilesController class handles files related access.
public class FilesController {
    public String upload() {
        try {
            UploadFile uf1 = pFile("file1");
            uf1.writeTo(applicationPath() + "/static/docs");
            flash("notice", "You have successfully uploaded a file.");

            setViewData("file", uf1.getFileName());
        } catch (Exception ex) {
            flash("error", "There is a problem with upload.");
        return null;

One of the most important file upload related APIs is the pFile(String key) method. It uses the value associated with the name attribute in the file upload jsp file and returns the uploaded file as an UploadFile instance.

UploadFile class

The UploadFile class contains methods for further processing of the uploaded file.

public String getFileName(); //get original uploaded file name
public InputStream getInputStream(); //get uploaded file as an input stream
public OutputStream getOutputStream(); //output uploaded file as an output stream
public long getSize(); //get the total number of bytes of the uploaded file
public void writeTo(File outputFile); //write the uploaded file to an output file
public void writeTo(String dirPath); //write the uploaded file to a directory

More APIs from ActionControl class

The ActionControl class contains some useful static methods for file uploads.

public static boolean isFileUploadRequest(); //check if the request is a file-upload request
public static List pFiles(); //get a list of upload files
public static Map pFilesMap(); //key is the name attribute in the HTTP form
public static List getUploadFilesAsFiles(String fileRepository); //store all uploaded files to a file repository

Controlling file uploads

Scooter provides the following configurable properties in file to control file uploads:

Property Description
upload.file.repository specifies a file directory for storing uploaded files when the item size exceed the threshold. The default value is null. specifies a threshold, in bytes, below which items will be retained in memory and above which they will be stored as a file. The default value is 10240 bytes. specifies maximum allowed bytes of a complete upload request. The default value of -1 indicates, that there is no limit.
maximum.bytes.per.uploaded.file specifies maximum allowed bytes of a single upload file. The default value of -1 indicates, that there is no limit.