1 16 17 package org.springframework.web.multipart.commons; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.Serializable ; 24 25 import org.apache.commons.fileupload.FileItem; 26 import org.apache.commons.fileupload.FileUploadException; 27 import org.apache.commons.fileupload.disk.DiskFileItem; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.springframework.web.multipart.MultipartFile; 32 33 45 public class CommonsMultipartFile implements MultipartFile, Serializable { 46 47 protected static final Log logger = LogFactory.getLog(CommonsMultipartFile.class); 48 49 private final FileItem fileItem; 50 51 private final long size; 52 53 54 58 public CommonsMultipartFile(FileItem fileItem) { 59 this.fileItem = fileItem; 60 this.size = this.fileItem.getSize(); 61 } 62 63 67 public final FileItem getFileItem() { 68 return this.fileItem; 69 } 70 71 72 public String getName() { 73 return this.fileItem.getFieldName(); 74 } 75 76 public String getOriginalFilename() { 77 String filename = this.fileItem.getName(); 78 if (filename == null) { 79 return ""; 81 } 82 int pos = filename.lastIndexOf("/"); 84 if (pos == -1) { 85 pos = filename.lastIndexOf("\\"); 87 } 88 if (pos != -1) { 89 return filename.substring(pos + 1); 91 } 92 else { 93 return filename; 95 } 96 } 97 98 public String getContentType() { 99 return this.fileItem.getContentType(); 100 } 101 102 public boolean isEmpty() { 103 return (this.size == 0); 104 } 105 106 public long getSize() { 107 return this.size; 108 } 109 110 public byte[] getBytes() { 111 if (!isAvailable()) { 112 throw new IllegalStateException ("File has been moved - cannot be read again"); 113 } 114 byte[] bytes = this.fileItem.get(); 115 return (bytes != null ? bytes : new byte[0]); 116 } 117 118 public InputStream getInputStream() throws IOException { 119 if (!isAvailable()) { 120 throw new IllegalStateException ("File has been moved - cannot be read again"); 121 } 122 InputStream inputStream = this.fileItem.getInputStream(); 123 return (inputStream != null ? inputStream : new ByteArrayInputStream (new byte[0])); 124 } 125 126 public void transferTo(File dest) throws IOException , IllegalStateException { 127 if (!isAvailable()) { 128 throw new IllegalStateException ("File has already been moved - cannot be transferred again"); 129 } 130 131 if (dest.exists() && !dest.delete()) { 132 throw new IOException ( 133 "Destination file [" + dest.getAbsolutePath() + "] already exists and could not be deleted"); 134 } 135 136 try { 137 this.fileItem.write(dest); 138 if (logger.isDebugEnabled()) { 139 String action = "transferred"; 140 if (!this.fileItem.isInMemory()) { 141 action = isAvailable() ? "copied" : "moved"; 142 } 143 logger.debug("Multipart file '" + getName() + "' with original filename [" + 144 getOriginalFilename() + "], stored " + getStorageDescription() + ": " + 145 action + " to [" + dest.getAbsolutePath() + "]"); 146 } 147 } 148 catch (FileUploadException ex) { 149 throw new IllegalStateException (ex.getMessage()); 150 } 151 catch (IOException ex) { 152 throw ex; 153 } 154 catch (Exception ex) { 155 logger.error("Could not transfer to file", ex); 156 throw new IOException ("Could not transfer to file: " + ex.getMessage()); 157 } 158 } 159 160 164 protected boolean isAvailable() { 165 if (this.fileItem.isInMemory()) { 167 return true; 168 } 169 if (this.fileItem instanceof DiskFileItem) { 171 return ((DiskFileItem) this.fileItem).getStoreLocation().exists(); 172 } 173 return (this.fileItem.getSize() == this.size); 175 } 176 177 182 public String getStorageDescription() { 183 if (this.fileItem.isInMemory()) { 184 return "in memory"; 185 } 186 else if (this.fileItem instanceof DiskFileItem) { 187 return "at [" + ((DiskFileItem) this.fileItem).getStoreLocation().getAbsolutePath() + "]"; 188 } 189 else { 190 return "on disk"; 191 } 192 } 193 194 } 195 | Popular Tags |