1 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 37 public class StringMultipartFileEditor extends PropertyEditorSupport { 38 39 40 private static final Log logger = LogFactory.getLog(StringMultipartFileEditor.class); 41 42 private final String charsetName; 43 44 45 48 public StringMultipartFileEditor() { 49 this.charsetName = null; 50 } 51 52 57 public StringMultipartFileEditor(String charsetName) { 58 this.charsetName = charsetName; 59 } 60 61 62 public void setAsText(String text) { 63 setValue(text); 64 } 65 66 public void setValue(Object value) { 67 if (value instanceof MultipartFile) { 68 MultipartFile multipartFile = (MultipartFile) value; 69 try { 70 super.setValue(this.charsetName != null ? 71 new String (multipartFile.getBytes(), this.charsetName) : 72 new String (multipartFile.getBytes())); 73 } 74 catch (IOException ex) { 75 logger.warn("Cannot read contents of multipart file", ex); 76 throw new IllegalArgumentException ("Cannot read contents of multipart file: " + ex.getMessage()); 77 } 78 } 79 else { 80 super.setValue(value); 81 } 82 } 83 84 } 85 | Popular Tags |