Example usage for org.apache.commons.fileupload.disk DiskFileItemFactory setFileCleaningTracker

List of usage examples for org.apache.commons.fileupload.disk DiskFileItemFactory setFileCleaningTracker

Introduction

In this page you can find the example usage for org.apache.commons.fileupload.disk DiskFileItemFactory setFileCleaningTracker.

Prototype

public void setFileCleaningTracker(FileCleaningTracker pTracker) 

Source Link

Document

Returns the tracker, which is responsible for deleting temporary files.

Usage

From source file:org.opensubsystems.core.util.servlet.WebParamUtils.java

/**
 * Parse multipart request and separate regular parameters and files. The
 * files names are also stored as values of the parameters that are used to 
 * upload them./*from  w w  w  .  j  a v a 2  s.  c  om*/
 * 
 * @param strLogPrefix - log prefix used for all log output to tie together
 *                       the same invocations
 * @param request - request to get parameter from
 * @return TwoElementStruct<Map<String, String>, Map<String, FileItem>> - the
 *                  first element is map of parameter names and their values. 
 *                  For uploaded files the files names are also stored here as 
 *                  values of the parameters that are used to upload them.
 *                  If there is only one value of the parameter then the value
 *                  is stored directly as String. If there are multiple values
 *                  then the values are stored as List<String>.
 *                  The second element is map of parameter names and the files
 *                  that are uploaded as these parameters.
 * @throws FileUploadException - an error has occurred
 */
public static TwoElementStruct<Map<String, Object>, Map<String, FileItem>> parseMultipartRequest(
        String strLogPrefix, HttpServletRequest request) throws FileUploadException {
    if (GlobalConstants.ERROR_CHECKING) {
        assert ServletFileUpload.isMultipartContent(request) : "Specified request is not multipart";
    }

    TwoElementStruct<Map<String, Object>, Map<String, FileItem>> returnValue;
    FileCleaningTracker fileCleaningTracker;
    String strTempDir;
    DiskFileItemFactory factory;
    Properties prpSettings;
    int iMaxInMemorySize;
    int iMaxSize;
    ServletFileUpload upload;
    List<FileItem> items;

    // TODO: Improve: Consider calling 
    // FileUtils.createTemporarySubdirectory
    // as done in legacy Formature.DocumentTemplateServlet.getFormToProcess
    // to store the temporary files per session and request
    strTempDir = FileUtils.getTemporaryDirectory();

    prpSettings = Config.getInstance().getProperties();
    iMaxInMemorySize = PropertyUtils.getIntPropertyInRange(prpSettings, REQUEST_UPLOAD_MEMORY_THRESHOLD,
            REQUEST_UPLOAD_MEMORY_THRESHOLD_DEFAULT, "Maximal size of uploaded file that is kept in memory", 1, // 0 is allowed 
            Integer.MAX_VALUE);
    iMaxSize = PropertyUtils.getIntPropertyInRange(prpSettings, REQUEST_UPLOAD_MAX_SIZE,
            REQUEST_UPLOAD_MAX_SIZE_DEFAULT, "Maximal size of uploaded file", 1, // 0 is allowed 
            Integer.MAX_VALUE);

    fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(request.getServletContext());

    // Create a factory for disk-based file items
    factory = new DiskFileItemFactory();
    factory.setFileCleaningTracker(fileCleaningTracker);
    // Set factory constraints
    factory.setSizeThreshold(iMaxInMemorySize);
    factory.setRepository(new File(strTempDir));

    // Create a new file upload handler
    upload = new ServletFileUpload(factory);
    // Set overall request size constraint
    upload.setSizeMax(iMaxSize);

    // Parse the request
    items = upload.parseRequest(request);
    if ((items != null) && (!items.isEmpty())) {
        Map mpParams;
        Map mpFiles;
        String strParamName;
        String strValue;
        Object temp;
        List<String> lstValues;

        mpParams = new HashMap(items.size());
        mpFiles = new HashMap();

        returnValue = new TwoElementStruct(mpParams, mpFiles);
        for (FileItem item : items) {
            strParamName = item.getFieldName();
            if (item.isFormField()) {
                strValue = item.getString();
            } else {
                strValue = item.getName();
                mpFiles.put(strParamName, item);
            }

            temp = mpParams.put(strParamName, strValue);
            if (temp != null) {
                // There was already an value so convert it to list of values
                if (temp instanceof String) {
                    // There are currently exactly two values
                    lstValues = new ArrayList<>();
                    lstValues.add((String) temp);
                    mpParams.put(strParamName, lstValues);
                } else {
                    // There are currently more than two values
                    lstValues = (List<String>) temp;
                }
                lstValues.add(strValue);
            }
        }
    } else {
        returnValue = new TwoElementStruct(Collections.emptyMap(), Collections.emptyMap());
    }

    return returnValue;
}

From source file:org.primefaces.webapp.filter.FileUploadFilter.java

protected FileItemFactory createFileItemFactory(HttpServletRequest httpServletRequest) {
    DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
    if (thresholdSize != null) {
        diskFileItemFactory.setSizeThreshold(Integer.valueOf(thresholdSize));
    }//  w ww  . j  a  va  2 s . c  om
    if (uploadDir != null) {
        diskFileItemFactory.setRepository(new File(uploadDir));
    }

    FileCleaningTracker fileCleaningTracker = FileCleanerCleanup
            .getFileCleaningTracker(httpServletRequest.getSession().getServletContext());
    if (fileCleaningTracker != null) {
        diskFileItemFactory.setFileCleaningTracker(fileCleaningTracker);
    }

    return diskFileItemFactory;
}

From source file:org.sc.probro.servlets.IndexCreatorServlet.java

public static DiskFileItemFactory newDiskFileItemFactory(ServletContext context, File repository) {

    FileCleaningTracker fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(context);

    DiskFileItemFactory factory = new DiskFileItemFactory(10 * MB, repository);
    factory.setFileCleaningTracker(fileCleaningTracker);

    return factory;
}