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

📄 fileuploadbase.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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.wicket.util.upload;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * <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>. * * <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 */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;	}	// ----------------------------------------------------- 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).	 */	public static final int MAX_HEADER_SIZE = 1024;	// ----------------------------------------------------------- Data members	/**	 * The maximum size permitted for an uploaded file. A value of -1 indicates no maximum.	 */	private long sizeMax = -1;	/**	 * The content encoding to use when reading part headers.	 */	private String headerEncoding;	// ----------------------------------------------------- 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 upload size.	 *	 * @return The maximum allowed size, in bytes.	 *	 * @see #setSizeMax(long)	 *	 */	public long getSizeMax()	{		return sizeMax;	}	/**	 * Sets the maximum allowed upload size. If negative, there is no maximum.	 *	 * @param sizeMax	 *            The maximum allowed size, in bytes, or -1 for no maximum.	 *	 * @see #getSizeMax()	 *	 */	public void setSizeMax(long sizeMax)	{		this.sizeMax = sizeMax;	}	/**	 * Retrieves the character encoding used when reading the headers of an individual part. When	 * 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 parts.	 * When 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 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.	 *	 * @exception FileUploadException	 *                if there are problems reading/parsing the request or storing files.	 */	public List /* FileItem */parseRequest(RequestContext ctx) throws FileUploadException	{		if (ctx == null)		{			throw new IllegalArgumentException("ctx parameter cannot be null");		}		ArrayList items = new ArrayList();		String contentType = ctx.getContentType();		if ((null == contentType) || (!contentType.toLowerCase().startsWith(MULTIPART)))		{			throw new InvalidContentTypeException("the request doesn't contain a " +					MULTIPART_FORM_DATA + " or " + MULTIPART_MIXED +					" stream, content type header is " + contentType);		}		int requestSize = ctx.getContentLength();		if (requestSize == -1)		{			throw new UnknownSizeException("the request was rejected because its size is unknown");		}		if (sizeMax >= 0 && requestSize > sizeMax)		{			throw new SizeLimitExceededException("the request was rejected because "					+ "its size exceeds allowed range");		}		try		{			byte[] boundary = getBoundary(contentType);			if (boundary == null)			{				throw new FileUploadException("the request was rejected because "						+ "no multipart boundary was found");			}			InputStream input = ctx.getInputStream();			MultipartFormInputStream multi = new MultipartFormInputStream(input, boundary);			multi.setHeaderEncoding(headerEncoding);			boolean nextPart = multi.skipPreamble();			// Don't allow a header larger than this size (to prevent DOS			// attacks)			final int maxHeaderBytes = 65536;			while (nextPart)			{				Map headers = parseHeaders(multi.readHeaders(maxHeaderBytes));				String fieldName = getFieldName(headers);				if (fieldName != null)				{					String subContentType = getHeader(headers, CONTENT_TYPE);					if (subContentType != null &&							subContentType.toLowerCase().startsWith(MULTIPART_MIXED))					{						// Multiple files.						byte[] subBoundary = getBoundary(subContentType);						multi.setBoundary(subBoundary);						boolean nextSubPart = multi.skipPreamble();						while (nextSubPart)						{							headers = parseHeaders(multi.readHeaders(maxHeaderBytes));							if (getFileName(headers) != null)							{								FileItem item = createItem(headers, false);                                items.add(item);								OutputStream os = item.getOutputStream();								try								{									multi.readBodyData(os);								}								finally								{									os.close();								}							}							else							{								// Ignore anything but files inside								// multipart/mixed.								multi.discardBodyData();							}							nextSubPart = multi.readBoundary();						}						multi.setBoundary(boundary);					}					else					{						FileItem item = createItem(headers, getFileName(headers) == null);                        items.add(item);						OutputStream os = item.getOutputStream();						try

⌨️ 快捷键说明

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