Java Code Examples for org.springframework.util.StringUtils#addStringToArray()

The following examples show how to use org.springframework.util.StringUtils#addStringToArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonsFileUploadSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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 FileItems 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<>();
	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);
			LogFormatUtils.traceDebug(logger, traceOn ->
					"Part '" + file.getName() + "', size " + file.getSize() +
							" bytes, filename='" + file.getOriginalFilename() + "'" +
							(traceOn ? ", storage=" + file.getStorageDescription() : "")
			);
		}
	}
	return new MultipartParsingResult(multipartFiles, multipartParameters, multipartParameterContentTypes);
}
 
Example 2
Source File: CommonsFileUploadSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * 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 FileItems 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<>();
	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);
			LogFormatUtils.traceDebug(logger, traceOn ->
					"Part '" + file.getName() + "', size " + file.getSize() +
							" bytes, filename='" + file.getOriginalFilename() + "'" +
							(traceOn ? ", storage=" + file.getStorageDescription() : "")
			);
		}
	}
	return new MultipartParsingResult(multipartFiles, multipartParameters, multipartParameterContentTypes);
}
 
Example 3
Source File: EgovMultipartResolver.java    From oslits with GNU General Public License v3.0 4 votes vote down vote up
/**
    * 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");
	    }
	}

    }
}

return new MultipartParsingResult(multipartFiles, multipartParameters, null);
   }
 
Example 4
Source File: CommonsFileUploadSupport.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 = 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);
}
 
Example 5
Source File: CommonsFileUploadSupport.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: MultiCommonsMultipartResolver.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
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 (log.isWarnEnabled()) {
                        log.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(fileItem.getName(), file);
            if (log.isDebugEnabled()) {
                log.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);
}
 
Example 7
Source File: MultiCommonsMultipartResolver.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
@Override
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 (log.isWarnEnabled()) {
                        log.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(fileItem.getName(), file);
            if (log.isDebugEnabled()) {
                log.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);
}