01 /* 02 * Copyright 2002-2004 the original author or authors. 03 * 04 * Licensed under the Apache License, Version 2.0 (the "License"); 05 * you may not use this file except in compliance with the License. 06 * You may obtain a copy of the License at 07 * 08 * http://www.apache.org/licenses/LICENSE-2.0 09 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.web.multipart.support; 18 19 import java.beans.PropertyEditorSupport; 20 import java.io.IOException; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import org.springframework.web.multipart.MultipartFile; 26 27 /** 28 * Custom PropertyEditor for converting MultipartFiles to Strings. 29 * Allows to specify the charset to use. 30 * @author Juergen Hoeller 31 * @since 13.10.2003 32 */ 33 public class StringMultipartFileEditor extends PropertyEditorSupport { 34 35 protected final Log logger = LogFactory.getLog(getClass()); 36 37 private String charsetName; 38 39 /** 40 * Create a new StringMultipartFileEditor, using the default charset. 41 */ 42 public StringMultipartFileEditor() { 43 } 44 45 /** 46 * Create a new StringMultipartFileEditor, using the given charset. 47 * @param charsetName valid charset name 48 * @see java.lang.String#String(byte[],String) 49 */ 50 public StringMultipartFileEditor(String charsetName) { 51 this.charsetName = charsetName; 52 } 53 54 public void setValue(Object value) { 55 if (value instanceof MultipartFile) { 56 MultipartFile multipartFile = (MultipartFile) value; 57 try { Rate58 super.setValue(this.charsetName != null ? 59 new String(multipartFile.getBytes(), this.charsetName) : 60 new String(multipartFile.getBytes())); 61 } 62 catch (IOException ex) { 63 logger.error("Cannot read contents of multipart file", ex); 64 throw new IllegalArgumentException("Cannot read contents of multipart file: " + ex.getMessage()); 65 } 66 } 67 } 68 69 }