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

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

Introduction

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

Prototype

@Override
    public String getName() 

Source Link

Usage

From source file:lcn.module.oltp.web.common.base.MultipartResolver.java

/**
 * multipart?  parsing? .//from w  w w .j  a  va  2  s  . com
 */
@SuppressWarnings("unchecked")
@Override
protected MultipartParsingResult parseFileItems(List fileItems, String encoding) {

    //? 3.0  
    MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<String, MultipartFile>();
    //   Map<String, String[]> multipartParameters = new HashMap<String, String[]>();

    //   Map multipartFiles = new HashMap();
    Map multipartParameters = new HashMap();

    // Extract multipart files and multipart parameters.
    for (Iterator it = fileItems.iterator(); it.hasNext();) {
        FileItem fileItem = (FileItem) it.next();

        if (fileItem.isFormField()) {

            String value = null;
            if (encoding != null) {
                try {
                    value = fileItem.getString(encoding);
                } catch (UnsupportedEncodingException ex) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Could not decode multipart item '" + fileItem.getFieldName()
                                + "' with encoding '" + encoding + "': using platform default");
                    }
                    value = fileItem.getString();
                }
            } else {
                value = fileItem.getString();
            }
            String[] curParam = (String[]) 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);
            }
        } else {

            if (fileItem.getSize() > 0) {
                // multipart file field
                CommonsMultipartFile file = new CommonsMultipartFile(fileItem);

                //? 3.0 ? API? 
                List<MultipartFile> fileList = new ArrayList<MultipartFile>();
                fileList.add(file);

                if (multipartFiles.put(fileItem.getName(), fileList) != null) { // CHANGED!!
                    throw new MultipartException("Multiple files for field name [" + file.getName()
                            + "] found - not supported by MultipartResolver");
                }
                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((MultiValueMap<String, MultipartFile>) multipartFiles,
            (Map<String, String[]>) multipartParameters, multipartParameters);

    //   return new MultipartParsingResult(multipartFiles, multipartParameters);
}

From source file:egovframework.asadal.asapro.com.cmm.web.AsaproEgovMultipartResolver.java

/**
 * multipart?  parsing? .//from   w ww . java2s .co  m
 */
@SuppressWarnings("unchecked")
@Override
protected MultipartParsingResult parseFileItems(List fileItems, String encoding) {

    //? 3.0  
    MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<String, MultipartFile>();
    Map<String, String[]> multipartParameters = new HashMap<String, String[]>();

    // Extract multipart files and multipart parameters.
    for (Iterator it = fileItems.iterator(); it.hasNext();) {
        FileItem fileItem = (FileItem) it.next();

        if (fileItem.isFormField()) {

            String value = null;
            if (encoding != null) {
                try {
                    value = fileItem.getString(encoding);
                } catch (UnsupportedEncodingException ex) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Could not decode multipart item '" + fileItem.getFieldName()
                                + "' with encoding '" + encoding + "': using platform default");
                    }
                    value = fileItem.getString();
                }
            } else {
                value = fileItem.getString();
            }
            String[] curParam = (String[]) 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);
            }
        } else {

            if (fileItem.getSize() > 0) {
                // multipart file field
                CommonsMultipartFile file = new CommonsMultipartFile(fileItem);

                //? 3.0 ? API? 
                List<MultipartFile> fileList = new ArrayList<MultipartFile>();
                fileList.add(file);

                if (!AsaproEgovWebUtil.checkAllowUploadFileExt(file.getOriginalFilename()))
                    throw new MultipartException(" ? ? .");

                if (multipartFiles.put(fileItem.getName(), fileList) != null) { // CHANGED!!
                    throw new MultipartException("Multiple files for field name [" + file.getName()
                            + "] found - not supported by MultipartResolver");
                }
                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, null);
}

From source file:com.gradecak.alfresco.simpless.MetadataManager.java

/**
 * handles only simple types (no associations ... complex types)
 * /*from w ww .  j a  va 2s  . c  o  m*/
 * any null properties will be removed from the node by calling nodeService.removeProperty, as
 * setting null is considered as a removal of the value
 * 
 * if a cm:content property is present than the value might be a file, a multipart, a ContentData
 * or a String. Otherwise the value must be <code>Serializable</code> However, before saving an
 * alfresco converter is executed on the vale for the given type repreented in the model
 * 
 * Any readonly properties will not be serialized/saved
 * 
 * return {@link SysBase} with converted values (did not check the repository) but without syncing
 * actions & aspects as we belongs to the same transaction and so the sync should be done after
 * the call to this method. As example we might consider behaviors that can change values of the
 * node
 */
public <T extends SysBase> T save(final T object) {
    Assert.notNull(object, "object must not be null");

    MetadataHolder holder = object.getMetadataHolder();
    Assert.notNull(holder, "the holder object must not be null");

    Map<QName, Serializable> allProps = holder.getPropertiesMap();

    // resolve unkown properties and put it all together
    Map<String, Serializable> unkwonProperties = object.getUnkwonProperties();
    for (Entry<String, Serializable> entry : unkwonProperties.entrySet()) {
        QName qname = QName.resolveToQName(serviceRegistry.getNamespaceService(), entry.getKey());
        allProps.put(qname, entry.getValue());
    }

    NodeRef toNode = object.getNodeRef();

    // works on latest alfresco versions. if
    // if (this.serviceRegistry.getCheckOutCheckInService().isCheckedOut(toNode)) {

    // works on all alfresco versions
    if (this.serviceRegistry.getCheckOutCheckInService().getWorkingCopy(toNode) != null) {
        if (LockStatus.LOCK_OWNER.equals(this.serviceRegistry.getLockService().getLockStatus(toNode))) {
            toNode = this.serviceRegistry.getCheckOutCheckInService().getWorkingCopy(toNode);
        }
    }

    Metadata properties = new Metadata();
    for (Entry<QName, Serializable> entry : allProps.entrySet()) {
        QName qname = entry.getKey();
        if (qname == null) {
            continue;
        }

        PropertyDefinition propertyDef = this.serviceRegistry.getDictionaryService().getProperty(qname);
        if (propertyDef == null) {
            continue;
        }

        if (holder.isReadOnlyProperty(qname)) {
            continue;
        }

        Serializable value = entry.getValue();

        if (value instanceof String[]) {
            // TODO: check if we need to handle props different than String[]
            value = ((String[]) value)[0];
        }

        DataTypeDefinition dataType = propertyDef.getDataType();
        if (DataTypeDefinition.CONTENT.equals(dataType.getName())) {

            if (toNode != null) {
                ContentWriter writer = this.serviceRegistry.getContentService().getWriter(toNode,
                        propertyDef.getName(), true);

                if (value instanceof CommonsMultipartFile) {
                    CommonsMultipartFile file = (CommonsMultipartFile) value;
                    writer.setMimetype(this.serviceRegistry.getMimetypeService()
                            .guessMimetype(file.getOriginalFilename()));
                    try {
                        writer.putContent(file.getInputStream());
                    } catch (IOException e) {
                        throw new ContentIOException("Could not write the file input stream", e);
                    }
                } else if (value instanceof File) {
                    File file = (File) value;
                    writer.setMimetype(this.serviceRegistry.getMimetypeService().guessMimetype(file.getName()));
                    writer.putContent(file);
                } else if (value instanceof String) {
                    writer.setMimetype("text/plain");
                    writer.putContent((String) value);
                } else if (value instanceof ContentData) {
                    properties.put(qname, (ContentData) value);
                } else {
                    if (value != null) {
                        throw new RuntimeException(
                                "Unhandled property of type d:content. Name : " + propertyDef.getName());
                    }
                }
            }
        } else {
            if (DataTypeDefinition.QNAME.equals(dataType.getName())) {
                if (value instanceof String && !StringUtils.hasText((String) value)) {
                    continue;
                }
            }

            Object convertedValue = converter.convert(dataType, value);
            if (convertedValue == null) {
                // TODO marked in order to verify this behavior
                // we remove the specific property as its value has been erased
                this.serviceRegistry.getNodeService().removeProperty(toNode, qname);
            } else {
                properties.put(qname, (Serializable) convertedValue);
                object.getMetadataHolder().setProperty(qname, (Serializable) convertedValue);
            }
        }
    }

    // assure to remove nodeRef property before saving
    properties.remove(ContentModel.PROP_NODE_REF);
    if (!properties.isEmpty()) {
        this.serviceRegistry.getNodeService().addProperties(toNode, properties);
    }

    syncAspectsAndActions(toNode, object);

    return object;
}

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)
 *///from   w w w. java 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()
 *///w w w . j av  a  2  s  .c om
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;//from w  w  w.j  av  a 2 s.  com

    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();
    }
}