com.jkthome.cmm.web.JkthomeMultipartResolver.java Source code

Java tutorial

Introduction

Here is the source code for com.jkthome.cmm.web.JkthomeMultipartResolver.java

Source

package com.jkthome.cmm.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.ArrayList;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.LinkedMultiValueMap;
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;

public class JkthomeMultipartResolver extends CommonsMultipartResolver {

    private static final Logger LOGGER = LoggerFactory.getLogger(JkthomeMultipartResolver.class);

    public JkthomeMultipartResolver() {
    }

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

    /**
     * multipart?  parsing? .
     */
    @SuppressWarnings("rawtypes")
    @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) {
                        LOGGER.warn(
                                "Could not decode multipart item '{}' with encoding '{}': using platform default",
                                fileItem.getFieldName(), encoding);
                        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");
                    }
                    LOGGER.debug(
                            "Found multipart file [{}] of size {} bytes with original filename [{}], stored {}",
                            file.getName(), file.getSize(), file.getOriginalFilename(),
                            file.getStorageDescription());
                }

            }
        }

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