multipartiterator.java
来自「这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用s」· Java 代码 · 共 456 行 · 第 1/2 页
JAVA
456 行
/*
* $Header: /sfroot/cvs/esimple/src/core/org/apache/struts/upload/MultipartIterator.java,v 1.1.1.1 2004/09/08 06:38:41 lava Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/09/08 06:38:41 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.upload;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
/**
* The MultipartIterator class is responsible for reading the
* input data of a multipart request and splitting it up into
* input elements, wrapped inside of a
* {@link org.apache.struts.upload.MultipartElement MultipartElement}
* for easy definition. To use this class, create a new instance
* of MultipartIterator passing it a HttpServletRequest in the
* constructor. Then use the {@link #getNextElement() getNextElement}
* method until it returns null, then you're finished. Example: <br>
* <pre>
* MultipartIterator iterator = new MultipartIterator(request);
* MultipartElement element;
*
* while ((element = iterator.getNextElement()) != null) {
* //do something with element
* }
* </pre>
*
* @see org.apache.struts.upload.MultipartElement
* @author Mike Schachter
*/
public class MultipartIterator
{
/**
* The default encoding of a text element if none is specified.
*/
private static final String DEFAULT_ENCODING = "iso-8859-1";
/**
* The size in bytes to copy of text data at a time.
*/
private static final int TEXT_BUFFER_SIZE = 1000;
/**
* The name of the Content-Type header.
*/
public static String HEADER_CONTENT_TYPE = "Content-Type";
/**
* The name of the Content-Disposition header.
*/
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
/**
* The exception message for when the boundary of a multipart request can't be determined.
*/
public static final String MESSAGE_CANNOT_RETRIEVE_BOUNDARY =
"MultipartIterator: cannot retrieve boundary for multipart request";
private static final String PARAMETER_BOUNDARY = "boundary=";
private static final String FILE_PREFIX = "strts";
/**
* The request instance for this class
*/
protected HttpServletRequest request;
/**
* The InputStream to use to read the multipart data.
*/
protected MultipartBoundaryInputStream inputStream;
/**
* The boundary for this multipart request
*/
protected String boundary;
/**
* The maximum file size in bytes allowed. Ignored if -1
*/
protected long maxSize = -1;
/**
* The content length of this request
*/
protected int contentLength;
/**
* The size in bytes written to the filesystem at a time [20K]
*/
protected int diskBufferSize = 2 * 10240;
/**
* The amount of data read from a request at a time.
* This also represents the maximum size in bytes of
* a line read from the request [4KB]
*/
protected int bufferSize = 4096;
/**
* The temporary directory to store files
*/
protected String tempDir;
/**
* The content-type.
*/
protected String contentType;
/**
* Whether the maximum length has been exceeded.
*/
protected boolean maxLengthExceeded;
/**
* Constructs a MultipartIterator with a default buffer size and no file size
* limit
*
* @param request The multipart request to iterate
*/
public MultipartIterator(HttpServletRequest request) throws IOException
{
this(request, -1);
}
/**
* Constructs a MultipartIterator with the specified buffer size and
* no file size limit
*
* @param request The multipart request to iterate
* @param bufferSize The size in bytes that should be read from the input
* stream at a times
*/
public MultipartIterator(HttpServletRequest request, int bufferSize) throws IOException
{
this (request, bufferSize, -1);
}
/**
* Constructs a MultipartIterator with the specified buffer size and
* the specified file size limit in bytes
*
* @param request The multipart request to iterate
* @param bufferSize The size in bytes that should be read from the input
* stream at a times
* @param maxSize The maximum size in bytes allowed for a multipart element's data
*/
public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize) throws IOException
{
this(request, bufferSize, maxSize, null);
}
public MultipartIterator(HttpServletRequest request, int bufferSize, long maxSize, String tempDir) throws IOException
{
this.request = request;
this.maxSize = maxSize;
if (bufferSize > -1)
{
this.bufferSize = bufferSize;
}
if (tempDir != null)
{
this.tempDir = tempDir;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?