⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileuploadbase.java

📁 apache commons-fileupload-1.2.jar
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.fileupload;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.NoSuchElementException;import javax.servlet.http.HttpServletRequest;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.fileupload.servlet.ServletRequestContext;import org.apache.commons.fileupload.util.Closeable;import org.apache.commons.fileupload.util.LimitedInputStream;import org.apache.commons.fileupload.util.Streams;/** * <p>High level API for processing file uploads.</p> * * <p>This class handles multiple files per single HTML widget, sent using * <code>multipart/mixed</code> encoding type, as specified by * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>.  Use {@link * #parseRequest(HttpServletRequest)} to acquire a list of {@link * org.apache.commons.fileupload.FileItem}s associated with a given HTML * widget.</p> * * <p>How the data for individual parts is stored is determined by the factory * used to create them; a given part may be in memory, on disk, or somewhere * else.</p> * * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> * @author <a href="mailto:dlr@collab.net">Daniel Rall</a> * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> * @author <a href="mailto:jmcnally@collab.net">John McNally</a> * @author <a href="mailto:martinc@apache.org">Martin Cooper</a> * @author Sean C. Sullivan * * @version $Id: FileUploadBase.java 502350 2007-02-01 20:42:48Z jochen $ */public abstract class FileUploadBase {    // ---------------------------------------------------------- Class methods    /**     * <p>Utility method that determines whether the request contains multipart     * content.</p>     *     * <p><strong>NOTE:</strong>This method will be moved to the     * <code>ServletFileUpload</code> class after the FileUpload 1.1 release.     * Unfortunately, since this method is static, it is not possible to     * provide its replacement until this method is removed.</p>     *     * @param ctx The request context to be evaluated. Must be non-null.     *     * @return <code>true</code> if the request is multipart;     *         <code>false</code> otherwise.     */    public static final boolean isMultipartContent(RequestContext ctx) {        String contentType = ctx.getContentType();        if (contentType == null) {            return false;        }        if (contentType.toLowerCase().startsWith(MULTIPART)) {            return true;        }        return false;    }    /**     * Utility method that determines whether the request contains multipart     * content.     *     * @param req The servlet request to be evaluated. Must be non-null.     *     * @return <code>true</code> if the request is multipart;     *         <code>false</code> otherwise.     *     * @deprecated Use the method on <code>ServletFileUpload</code> instead.     */    public static boolean isMultipartContent(HttpServletRequest req) {        return ServletFileUpload.isMultipartContent(req);    }    // ----------------------------------------------------- Manifest constants    /**     * HTTP content type header name.     */    public static final String CONTENT_TYPE = "Content-type";    /**     * HTTP content disposition header name.     */    public static final String CONTENT_DISPOSITION = "Content-disposition";    /**     * Content-disposition value for form data.     */    public static final String FORM_DATA = "form-data";    /**     * Content-disposition value for file attachment.     */    public static final String ATTACHMENT = "attachment";    /**     * Part of HTTP content type header.     */    public static final String MULTIPART = "multipart/";    /**     * HTTP content type header for multipart forms.     */    public static final String MULTIPART_FORM_DATA = "multipart/form-data";    /**     * HTTP content type header for multiple uploads.     */    public static final String MULTIPART_MIXED = "multipart/mixed";    /**     * The maximum length of a single header line that will be parsed     * (1024 bytes).     * @deprecated This constant is no longer used. As of commons-fileupload     *   1.2, the only applicable limit is the total size of a parts headers,     *   {@link MultipartStream#HEADER_PART_SIZE_MAX}.     */    public static final int MAX_HEADER_SIZE = 1024;    // ----------------------------------------------------------- Data members    /**     * The maximum size permitted for the complete request, as opposed to     * {@link #fileSizeMax}. A value of -1 indicates no maximum.     */    private long sizeMax = -1;    /**     * The maximum size permitted for a single uploaded file, as opposed     * to {@link #sizeMax}. A value of -1 indicates no maximum.     */    private long fileSizeMax = -1;    /**     * The content encoding to use when reading part headers.     */    private String headerEncoding;    /**     * The progress listener.     */    private ProgressListener listener;    // ----------------------------------------------------- Property accessors    /**     * Returns the factory class used when creating file items.     *     * @return The factory class for new file items.     */    public abstract FileItemFactory getFileItemFactory();    /**     * Sets the factory class to use when creating file items.     *     * @param factory The factory class for new file items.     */    public abstract void setFileItemFactory(FileItemFactory factory);    /**     * Returns the maximum allowed size of a complete request, as opposed     * to {@link #getFileSizeMax()}.     *     * @return The maximum allowed size, in bytes. The default value of     *   -1 indicates, that there is no limit.     *     * @see #setSizeMax(long)     *     */    public long getSizeMax() {        return sizeMax;    }    /**     * Sets the maximum allowed size of a complete request, as opposed     * to {@link #setFileSizeMax(long)}.     *     * @param sizeMax The maximum allowed size, in bytes. The default value of     *   -1 indicates, that there is no limit.     *     * @see #getSizeMax()     *     */    public void setSizeMax(long sizeMax) {        this.sizeMax = sizeMax;    }    /**     * Returns the maximum allowed size of a single uploaded file,     * as opposed to {@link #getSizeMax()}.     *     * @see #setFileSizeMax(long)     * @return Maximum size of a single uploaded file.     */    public long getFileSizeMax() {        return fileSizeMax;    }    /**     * Sets the maximum allowed size of a single uploaded file,     * as opposed to {@link #getSizeMax()}.     *     * @see #getFileSizeMax()     * @param fileSizeMax Maximum size of a single uploaded file.     */    public void setFileSizeMax(long fileSizeMax) {        this.fileSizeMax = fileSizeMax;    }    /**     * Retrieves the character encoding used when reading the headers of an     * individual part. When not specified, or <code>null</code>, the request     * encoding is used. If that is also not specified, or <code>null</code>,     * the platform default encoding is used.     *     * @return The encoding used to read part headers.     */    public String getHeaderEncoding() {        return headerEncoding;    }    /**     * Specifies the character encoding to be used when reading the headers of     * individual part. When not specified, or <code>null</code>, the request     * encoding is used. If that is also not specified, or <code>null</code>,     * the platform default encoding is used.     *     * @param encoding The encoding used to read part headers.     */    public void setHeaderEncoding(String encoding) {        headerEncoding = encoding;    }    // --------------------------------------------------------- Public methods    /**     * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>     * compliant <code>multipart/form-data</code> stream.     *     * @param req The servlet request to be parsed.     *     * @return A list of <code>FileItem</code> instances parsed from the     *         request, in the order that they were transmitted.     *     * @throws FileUploadException if there are problems reading/parsing     *                             the request or storing files.     *     * @deprecated Use the method in <code>ServletFileUpload</code> instead.     */    public List /* FileItem */ parseRequest(HttpServletRequest req)    throws FileUploadException {        return parseRequest(new ServletRequestContext(req));    }    /**     * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>     * compliant <code>multipart/form-data</code> stream.     *     * @param ctx The context for the request to be parsed.     *     * @return An iterator to instances of <code>FileItemStream</code>     *         parsed from the request, in the order that they were     *         transmitted.     *     * @throws FileUploadException if there are problems reading/parsing     *                             the request or storing files.     * @throws IOException An I/O error occurred. This may be a network     *   error while communicating with the client or a problem while     *   storing the uploaded content.     */    public FileItemIterator getItemIterator(RequestContext ctx)    throws FileUploadException, IOException {        return new FileItemIteratorImpl(ctx);    }    /**     * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>     * compliant <code>multipart/form-data</code> stream.     *     * @param ctx The context for the request to be parsed.     *     * @return A list of <code>FileItem</code> instances parsed from the     *         request, in the order that they were transmitted.     *     * @throws FileUploadException if there are problems reading/parsing     *                             the request or storing files.     */    public List /* FileItem */ parseRequest(RequestContext ctx)            throws FileUploadException {        try {            FileItemIterator iter = getItemIterator(ctx);            List items = new ArrayList();            FileItemFactory fac = getFileItemFactory();            if (fac == null) {                throw new NullPointerException(                    "No FileItemFactory has been set.");            }            while (iter.hasNext()) {                FileItemStream item = iter.next();                FileItem fileItem = fac.createItem(item.getFieldName(),                        item.getContentType(), item.isFormField(),                        item.getName());                try {                    Streams.copy(item.openStream(), fileItem.getOutputStream(),                            true);                } catch (FileUploadIOException e) {                    throw (FileUploadException) e.getCause();                } catch (IOException e) {                    throw new IOFileUploadException(                            "Processing of " + MULTIPART_FORM_DATA                            + " request failed. " + e.getMessage(), e);                }                items.add(fileItem);            }            return items;        } catch (FileUploadIOException e) {            throw (FileUploadException) e.getCause();        } catch (IOException e) {            throw new FileUploadException(e.getMessage(), e);        }    }    // ------------------------------------------------------ Protected methods    /**     * Retrieves the boundary from the <code>Content-type</code> header.     *     * @param contentType The value of the content type header from which to     *                    extract the boundary value.     *     * @return The boundary, as a byte array.     */    protected byte[] getBoundary(String contentType) {        ParameterParser parser = new ParameterParser();        parser.setLowerCaseNames(true);        // Parameter parser can handle null input        Map params = parser.parse(contentType, ';');        String boundaryStr = (String) params.get("boundary");        if (boundaryStr == null) {            return null;        }        byte[] boundary;        try {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -