egovframework.oe1.cms.com.web.EgovOe1MultipartResolver.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.oe1.cms.com.web.EgovOe1MultipartResolver.java

Source

/*
 * Copyright 2010 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package egovframework.oe1.cms.com.web;

/*
 * Copyright 2001-2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the ";License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS"; BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

/**
 * ? ?    ?
 * @author ?1 ?
 * @since 2010.07.20
 * @version 1.0
 * @see
 *
 * <pre>
 * << ?(Modification Information) >>
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2010.07.20  ?           ?
 *
 * Copyright (C) 2009 by MOPAS  All right reserved.
 * </pre>
 */
public class EgovOe1MultipartResolver extends CommonsMultipartResolver {
    /** log */
    protected static final Log LOG = LogFactory.getLog(EgovOe1MultipartResolver.class);

    /**
     * EgovOe1MultipartResolver ??
     */
    public EgovOe1MultipartResolver() {
    }

    /**
     * ?   multipart resolver ?.
     * 
     * @param servletContext
     */
    public EgovOe1MultipartResolver(ServletContext servletContext) {
        super(servletContext);
    }

    /**
     * multipart?  parsing? .
     */
    @SuppressWarnings("unchecked")
    @Override
    protected MultipartParsingResult parseFileItems(List fileItems, String encoding) {
        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);
                    if (multipartFiles.put(fileItem.getName(), file) != 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);
    }
}