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

The following are Jave code examples for showing how to use addStringToArray() of the org.springframework.util.StringUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: CommonsFileUploadSupport.java   View Source Code Vote up 4 votes
/**
 * 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);
}