Example usage for org.springframework.web.multipart.commons CommonsMultipartFile getStorageDescription

List of usage examples for org.springframework.web.multipart.commons CommonsMultipartFile getStorageDescription

Introduction

In this page you can find the example usage for org.springframework.web.multipart.commons CommonsMultipartFile getStorageDescription.

Prototype

public String getStorageDescription() 

Source Link

Document

Return a description for the storage location of the multipart content.

Usage

From source file:org.springframework.web.multipart.commons.CommonsFileUploadSupport.java

/**
 * Parse the given List of Commons FileItems into a Spring MultipartParsingResult,
 * containing Spring MultipartFile instances and a Map of multipart parameter.
 * @param fileItems the Commons FileIterms to parse
 * @param encoding the encoding to use for form fields
 * @return the Spring MultipartParsingResult
 * @see CommonsMultipartFile#CommonsMultipartFile(org.apache.commons.fileupload.FileItem)
 *///  w ww . j  a va  2  s  . c o m
protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String encoding) {
    MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<>();
    Map<String, String[]> multipartParameters = new HashMap<>();
    Map<String, String> multipartParameterContentTypes = new HashMap<>();

    // Extract multipart files and multipart parameters.
    for (FileItem fileItem : fileItems) {
        if (fileItem.isFormField()) {
            String value;
            String partEncoding = determineEncoding(fileItem.getContentType(), encoding);
            try {
                value = fileItem.getString(partEncoding);
            } catch (UnsupportedEncodingException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Could not decode multipart item '" + fileItem.getFieldName()
                            + "' with encoding '" + partEncoding + "': using platform default");
                }
                value = fileItem.getString();
            }
            String[] curParam = multipartParameters.get(fileItem.getFieldName());
            if (curParam == null) {
                // simple form field
                multipartParameters.put(fileItem.getFieldName(), new String[] { value });
            } else {
                // array of simple form fields
                String[] newParam = StringUtils.addStringToArray(curParam, value);
                multipartParameters.put(fileItem.getFieldName(), newParam);
            }
            multipartParameterContentTypes.put(fileItem.getFieldName(), fileItem.getContentType());
        } else {
            // multipart file field
            CommonsMultipartFile file = createMultipartFile(fileItem);
            multipartFiles.add(file.getName(), file);
            if (logger.isDebugEnabled()) {
                logger.debug("Found multipart file [" + file.getName() + "] of size " + file.getSize()
                        + " bytes with original filename [" + file.getOriginalFilename() + "], stored "
                        + file.getStorageDescription());
            }
        }
    }
    return new MultipartParsingResult(multipartFiles, multipartParameters, multipartParameterContentTypes);
}

From source file:org.springframework.web.multipart.commons.CommonsFileUploadSupport.java

/**
 * Cleanup the Spring MultipartFiles created during multipart parsing,
 * potentially holding temporary data on disk.
 * <p>Deletes the underlying Commons FileItem instances.
 * @param multipartFiles Collection of MultipartFile instances
 * @see org.apache.commons.fileupload.FileItem#delete()
 *///from  w  w  w . j av a  2 s .  c  o  m
protected void cleanupFileItems(MultiValueMap<String, MultipartFile> multipartFiles) {
    for (List<MultipartFile> files : multipartFiles.values()) {
        for (MultipartFile file : files) {
            if (file instanceof CommonsMultipartFile) {
                CommonsMultipartFile cmf = (CommonsMultipartFile) file;
                cmf.getFileItem().delete();
                if (logger.isDebugEnabled()) {
                    logger.debug("Cleaning up multipart file [" + cmf.getName() + "] with original filename ["
                            + cmf.getOriginalFilename() + "], stored " + cmf.getStorageDescription());
                }
            }
        }
    }
}

From source file:ubic.gemma.web.util.upload.CommonsMultipartMonitoredResolver.java

@Override
public void cleanupMultipart(MultipartHttpServletRequest request) {

    if (request instanceof FailedMultipartHttpServletRequest)
        return;//  w w  w  . j  a va  2  s  .c o m

    Map<String, MultipartFile> multipartFiles = request.getFileMap();
    for (MultipartFile multipartFile : multipartFiles.values()) {
        CommonsMultipartFile file = (CommonsMultipartFile) multipartFile;
        if (logger.isDebugEnabled()) {
            logger.debug("Cleaning up multipart file [" + file.getName() + "] with original filename ["
                    + file.getOriginalFilename() + "], stored " + file.getStorageDescription());
        }
        file.getFileItem().delete();
    }
}