1 16 17 package org.springframework.web.multipart.cos; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 31 import com.oreilly.servlet.MultipartRequest; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 35 import org.springframework.util.FileCopyUtils; 36 import org.springframework.web.multipart.MultipartFile; 37 import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest; 38 39 52 public class CosMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest { 53 54 protected static final Log logger = LogFactory.getLog(CosMultipartHttpServletRequest.class); 55 56 private final MultipartRequest multipartRequest; 57 58 59 64 protected CosMultipartHttpServletRequest(HttpServletRequest request, MultipartRequest multipartRequest) { 65 super(request); 66 this.multipartRequest = multipartRequest; 67 setMultipartFiles(initFileMap(multipartRequest)); 68 } 69 70 74 public MultipartRequest getMultipartRequest() { 75 return multipartRequest; 76 } 77 78 85 private Map initFileMap(MultipartRequest multipartRequest) { 86 Map files = new HashMap (); 87 Enumeration fileNames = multipartRequest.getFileNames(); 88 while (fileNames.hasMoreElements()) { 89 String fileName = (String ) fileNames.nextElement(); 90 files.put(fileName, new CosMultipartFile(fileName)); 91 } 92 return files; 93 } 94 95 96 public Enumeration getParameterNames() { 97 return this.multipartRequest.getParameterNames(); 98 } 99 100 public String getParameter(String name) { 101 return this.multipartRequest.getParameter(name); 102 } 103 104 public String [] getParameterValues(String name) { 105 return this.multipartRequest.getParameterValues(name); 106 } 107 108 public Map getParameterMap() { 109 Map params = new HashMap (); 110 Enumeration names = getParameterNames(); 111 while (names.hasMoreElements()) { 112 String name = (String ) names.nextElement(); 113 params.put(name, getParameterValues(name)); 114 } 115 return Collections.unmodifiableMap(params); 116 } 117 118 119 123 private class CosMultipartFile implements MultipartFile { 124 125 private final String name; 126 127 private final File file; 128 129 private final long size; 130 131 public CosMultipartFile(String name) { 132 this.name = name; 133 this.file = multipartRequest.getFile(this.name); 134 this.size = (this.file != null ? this.file.length() : 0); 135 } 136 137 public String getName() { 138 return name; 139 } 140 141 public String getOriginalFilename() { 142 String filename = multipartRequest.getOriginalFileName(this.name); 143 return (filename != null ? filename : ""); 144 } 145 146 public String getContentType() { 147 return multipartRequest.getContentType(this.name); 148 } 149 150 public boolean isEmpty() { 151 return (this.size == 0); 152 } 153 154 public long getSize() { 155 return this.size; 156 } 157 158 public byte[] getBytes() throws IOException { 159 if (this.file != null && !this.file.exists()) { 160 throw new IllegalStateException ("File has been moved - cannot be read again"); 161 } 162 return (this.size > 0 ? FileCopyUtils.copyToByteArray(this.file) : new byte[0]); 163 } 164 165 public InputStream getInputStream() throws IOException { 166 if (this.file != null && !this.file.exists()) { 167 throw new IllegalStateException ("File has been moved - cannot be read again"); 168 } 169 if (this.size != 0) { 170 return new FileInputStream (this.file); 171 } 172 else { 173 return new ByteArrayInputStream (new byte[0]); 174 } 175 } 176 177 public void transferTo(File dest) throws IOException , IllegalStateException { 178 if (this.file != null && !this.file.exists()) { 179 throw new IllegalStateException ("File has already been moved - cannot be transferred again"); 180 } 181 182 if (dest.exists() && !dest.delete()) { 183 throw new IOException ( 184 "Destination file [" + dest.getAbsolutePath() + "] already exists and could not be deleted"); 185 } 186 187 boolean moved = false; 188 if (this.file != null) { 189 moved = this.file.renameTo(dest); 190 if (!moved) { 191 FileCopyUtils.copy(this.file, dest); 192 } 193 } 194 else { 195 dest.createNewFile(); 196 } 197 198 if (logger.isDebugEnabled()) { 199 logger.debug("Multipart file '" + getName() + "' with original filename [" + 200 getOriginalFilename() + "], stored " + 201 (this.file != null ? "at [" + this.file.getAbsolutePath() + "]" : "in memory") + 202 ": " + (moved ? "moved" : "copied") + " to [" + dest.getAbsolutePath() + "]"); 203 } 204 } 205 } 206 207 } 208 | Popular Tags |