1 16 17 package org.springframework.web.multipart.support; 18 19 import java.io.IOException ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import org.springframework.beans.propertyeditors.ByteArrayPropertyEditor; 25 import org.springframework.web.multipart.MultipartFile; 26 27 33 public class ByteArrayMultipartFileEditor extends ByteArrayPropertyEditor { 34 35 36 private static final Log logger = LogFactory.getLog(ByteArrayMultipartFileEditor.class); 37 38 39 public void setValue(Object value) { 40 if (value instanceof MultipartFile) { 41 MultipartFile multipartFile = (MultipartFile) value; 42 try { 43 super.setValue(multipartFile.getBytes()); 44 } 45 catch (IOException ex) { 46 logger.warn("Cannot read contents of multipart file", ex); 47 throw new IllegalArgumentException ("Cannot read contents of multipart file: " + ex.getMessage()); 48 } 49 } 50 else if (value instanceof byte[]) { 51 super.setValue(value); 52 } 53 else { 54 super.setValue(value != null ? value.toString().getBytes() : null); 55 } 56 } 57 58 public String getAsText() { 59 byte[] value = (byte[]) getValue(); 60 return (value != null ? new String (value) : ""); 61 } 62 63 } 64 | Popular Tags |