1 18 19 package org.apache.struts.upload; 20 21 import java.io.BufferedOutputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 53 public class MultipartIterator 54 { 55 56 59 private static final String DEFAULT_ENCODING = "iso-8859-1"; 60 61 64 private static final int TEXT_BUFFER_SIZE = 1000; 65 66 69 public static String HEADER_CONTENT_TYPE = "Content-Type"; 70 71 74 public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition"; 75 76 79 public static final String MESSAGE_CANNOT_RETRIEVE_BOUNDARY = 80 "MultipartIterator: cannot retrieve boundary for multipart request"; 81 82 private static final String PARAMETER_BOUNDARY = "boundary="; 83 84 private static final String FILE_PREFIX = "strts"; 85 86 89 protected HttpServletRequest request; 90 91 94 protected MultipartBoundaryInputStream inputStream; 95 96 99 protected String boundary; 100 101 104 protected long maxSize = -1; 105 106 109 protected int contentLength; 110 111 114 protected int diskBufferSize = 2 * 10240; 115 116 121 protected int bufferSize = 4096; 122 123 126 protected String tempDir; 127 128 131 protected String contentType; 132 133 136 protected boolean maxLengthExceeded; 137 138 144 public MultipartIterator(HttpServletRequest request) throws IOException 145 { 146 this(request, -1); 147 } 148 149 157 public MultipartIterator(HttpServletRequest request, int bufferSize) throws IOException 158 { 159 this (request, bufferSize, -1); 160 } 161 162 171 public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize) throws IOException 172 { 173 this(request, bufferSize, maxSize, null); 174 } 175 176 public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize, String tempDir) throws IOException 177 { 178 this.request = request; 179 this.maxSize = maxSize; 180 if (bufferSize > -1) 181 { 182 this.bufferSize = bufferSize; 183 } 184 if (tempDir != null) 185 { 186 this.tempDir = tempDir; 187 } 188 else 189 { 190 this.tempDir = System.getProperty("java.io.tmpdir"); 192 } 193 this.maxLengthExceeded = false; 194 this.inputStream = new MultipartBoundaryInputStream(); 195 parseRequest(); 196 } 197 198 201 protected void parseRequest() throws IOException 202 { 203 getContentTypeOfRequest(); 205 this.contentLength = this.request.getContentLength(); 208 getBoundaryFromContentType(); 210 this.inputStream.setMaxLength(this.contentLength+1); 212 if ((this.maxSize > -1) && (this.contentLength > this.maxSize)) 214 { 215 this.maxLengthExceeded = true; 216 } 217 else 218 { 219 InputStream requestInputStream = this.request.getInputStream(); 220 if (requestInputStream.markSupported()) 222 { 223 requestInputStream.mark(contentLength+1); 224 } 225 this.inputStream.setBoundary(this.boundary); 226 this.inputStream.setInputStream(requestInputStream); 227 } 228 } 229 230 239 public MultipartElement getNextElement() throws IOException 240 { 241 MultipartElement element = null; 243 if (!isMaxLengthExceeded()) 244 { 245 if (!this.inputStream.isFinalBoundaryEncountered()) 246 { 247 if (this.inputStream.isElementFile()) 248 { 249 element = createFileMultipartElement(); 251 } 252 else 254 { 255 String encoding = getElementEncoding(); 256 element = createTextMultipartElement(encoding); 257 } 258 this.inputStream.resetForNextBoundary(); 259 } 260 } 261 return element; 262 } 263 264 267 protected String getElementEncoding() 268 { 269 String encoding = this.inputStream.getElementCharset(); 270 if (encoding == null) 271 { 272 encoding = this.request.getCharacterEncoding(); 273 if (encoding == null) 274 { 275 encoding = DEFAULT_ENCODING; 276 } 277 } 278 return encoding; 279 } 280 281 285 protected MultipartElement createTextMultipartElement(String encoding) throws IOException 286 { 287 MultipartElement element; 288 289 int read = 0; 290 byte[] buffer = new byte[TEXT_BUFFER_SIZE]; 291 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 292 while ((read = this.inputStream.read(buffer, 0, TEXT_BUFFER_SIZE)) > 0) 293 { 294 baos.write(buffer, 0, read); 295 } 296 String value = baos.toString(encoding); 298 element = new MultipartElement(this.inputStream.getElementName(), value); 299 return element; 300 } 301 302 305 protected MultipartElement createFileMultipartElement() throws IOException 306 { 307 MultipartElement element; 308 File elementFile = createLocalFile(); 310 element = new MultipartElement(this.inputStream.getElementName(), this.inputStream.getElementFileName(), 311 this.inputStream.getElementContentType(), elementFile); 312 return element; 313 } 314 315 320 public void setBufferSize(int bufferSize) { 321 this.bufferSize = bufferSize; 322 } 323 324 329 public int getBufferSize() { 330 return bufferSize; 331 } 332 333 338 public void setMaxSize(long maxSize) { 339 this.maxSize = maxSize; 340 } 341 342 346 public long getMaxSize() 347 { 348 return this.maxSize; 349 } 350 351 354 public boolean isMaxLengthExceeded() 355 { 356 return (this.maxLengthExceeded || this.inputStream.isMaxLengthMet()); 357 } 358 359 360 363 private final void getBoundaryFromContentType() throws IOException 364 { 365 if (this.contentType.lastIndexOf(PARAMETER_BOUNDARY) != -1) 366 { 367 String _boundary = this.contentType.substring(this.contentType.lastIndexOf(PARAMETER_BOUNDARY) + 9); 368 if (_boundary.endsWith("\n")) 369 { 370 this.boundary = _boundary.substring(0, _boundary.length()-1); 372 } 373 this.boundary = _boundary; 374 } 375 else 376 { 377 this.boundary = null; 378 } 379 if ((this.boundary == null) || (this.boundary.length() < 1)) 381 { 382 throw new IOException (MESSAGE_CANNOT_RETRIEVE_BOUNDARY); 383 } 384 } 385 388 private final void getContentTypeOfRequest() 389 { 390 this.contentType = request.getContentType(); 391 if (this.contentType == null) 392 { 393 this.contentType = this.request.getHeader(HEADER_CONTENT_TYPE); 394 } 395 } 396 397 400 protected File createLocalFile() throws IOException 401 { 402 File tempFile = File.createTempFile(FILE_PREFIX, null, new File (this.tempDir)); 403 BufferedOutputStream fos = new BufferedOutputStream (new FileOutputStream (tempFile), this.diskBufferSize); 404 int read = 0; 405 byte buffer[] = new byte[this.diskBufferSize]; 406 while ((read = this.inputStream.read(buffer, 0, this.diskBufferSize)) > 0) 407 { 408 fos.write(buffer, 0, read); 409 } 410 fos.flush(); 411 fos.close(); 412 return tempFile; 413 } 414 } 415 | Popular Tags |