/**
   * 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)
   */
  protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String encoding) {
    MultiValueMap<String, MultipartFile> multipartFiles =
        new LinkedMultiValueMap<String, MultipartFile>();
    Map<String, String[]> multipartParameters = new HashMap<String, String[]>();
    Map<String, String> multipartParameterContentTypes = new HashMap<String, String>();

    // Extract multipart files and multipart parameters.
    for (FileItem fileItem : fileItems) {
      if (fileItem.isFormField()) {
        String value;
        String partEncoding = determineEncoding(fileItem.getContentType(), encoding);
        if (partEncoding != null) {
          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();
          }
        } else {
          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 = new CommonsMultipartFile(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);
  }
Esempio n. 2
0
  /** 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(multipartFiles, multipartParameters);
  }