📄 commonsmultipartrequesthandler.java
字号:
/*
* $Header: /sfroot/cvs/esimple/src/core/org/apache/struts/upload/CommonsMultipartRequestHandler.java,v 1.1.1.1 2004/09/08 06:38:40 lava Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/09/08 06:38:40 $
*
* ====================================================================
*
* 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.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.Globals;
/**
* This class implements the <code>MultipartRequestHandler</code> interface
* by providing a wrapper around the Jakarta Commons FileUpload library.
*
* @author Martin Cooper
* @version $Revision: 1.1.1.1 $ $Date: 2004/09/08 06:38:40 $
* @since Struts 1.1
*/
public class CommonsMultipartRequestHandler implements MultipartRequestHandler {
// ----------------------------------------------------- Manifest Constants
/**
* The default value for the maximum allowable size, in bytes, of an
* uploaded file. The value is equivalent to 250MB.
*/
public static final long DEFAULT_SIZE_MAX = 250 * 1024 * 1024;
/**
* The default value for the threshold which determines whether an uploaded
* file will be written to disk or cached in memory. The value is equivalent
* to 250KB.
*/
public static final int DEFAULT_SIZE_THRESHOLD = 256 * 1024;
// ----------------------------------------------------- Instance Variables
/**
* Commons Logging instance.
*/
protected static Log log = LogFactory.getLog(
CommonsMultipartRequestHandler.class);
/**
* The combined text and file request parameters.
*/
private Hashtable elementsAll;
/**
* The file request parameters.
*/
private Hashtable elementsFile;
/**
* The text request parameters.
*/
private Hashtable elementsText;
/**
* The action mapping with which this handler is associated.
*/
private ActionMapping mapping;
/**
* The servlet with which this handler is associated.
*/
private ActionServlet servlet;
// ---------------------------------------- MultipartRequestHandler Methods
/**
* Retrieves the servlet with which this handler is associated.
*
* @return The associated servlet.
*/
public ActionServlet getServlet() {
return this.servlet;
}
/**
* Sets the servlet with which this handler is associated.
*
* @param servlet The associated servlet.
*/
public void setServlet(ActionServlet servlet) {
this.servlet = servlet;
}
/**
* Retrieves the action mapping with which this handler is associated.
*
* @return The associated action mapping.
*/
public ActionMapping getMapping() {
return this.mapping;
}
/**
* Sets the action mapping with which this handler is associated.
*
* @param mapping The associated action mapping.
*/
public void setMapping(ActionMapping mapping) {
this.mapping = mapping;
}
/**
* Parses the input stream and partitions the parsed items into a set of
* form fields and a set of file items. In the process, the parsed items
* are translated from Commons FileUpload <code>FileItem</code> instances
* to Struts <code>FormFile</code> instances.
*
* @param request The multipart request to be processed.
*
* @throws ServletException if an unrecoverable error occurs.
*/
public void handleRequest(HttpServletRequest request)
throws ServletException {
// Get the app config for the current request.
ModuleConfig ac = (ModuleConfig) request.getAttribute(
Globals.MODULE_KEY);
// Create and configure a DIskFileUpload instance.
DiskFileUpload upload = new DiskFileUpload();
// Set the maximum size before a FileUploadException will be thrown.
upload.setSizeMax((int) getSizeMax(ac));
// Set the maximum size that will be stored in memory.
upload.setSizeThreshold((int) getSizeThreshold(ac));
// Set the the location for saving data on disk.
upload.setRepositoryPath(getRepositoryPath(ac));
// Create the hash tables to be populated.
elementsText = new Hashtable();
elementsFile = new Hashtable();
elementsAll = new Hashtable();
// Parse the request into file items.
List items = null;
try {
items = upload.parseRequest(request);
} catch (DiskFileUpload.SizeLimitExceededException e) {
// Special handling for uploads that are too big.
request.setAttribute(
MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED,
Boolean.TRUE);
return;
} catch (FileUploadException e) {
log.error("Failed to parse multipart request", e);
throw new ServletException(e);
}
// Partition the items into form fields and files.
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
addTextParameter(request, item);
} else {
addFileParameter(item);
}
}
}
/**
* Returns a hash table containing the text (that is, non-file) request
* parameters.
*
* @return The text request parameters.
*/
public Hashtable getTextElements() {
return this.elementsText;
}
/**
* Returns a hash table containing the file (that is, non-text) request
* parameters.
*
* @return The file request parameters.
*/
public Hashtable getFileElements() {
return this.elementsFile;
}
/**
* Returns a hash table containing both text and file request parameters.
*
* @return The text and file request parameters.
*/
public Hashtable getAllElements() {
return this.elementsAll;
}
/**
* Cleans up when a problem occurs during request processing.
*/
public void rollback() {
Iterator iter = elementsFile.values().iterator();
while (iter.hasNext()) {
FormFile formFile = (FormFile) iter.next();
formFile.destroy();
}
}
/**
* Cleans up at the end of a request.
*/
public void finish() {
rollback();
}
// -------------------------------------------------------- Support Methods
/**
* Returns the maximum allowable size, in bytes, of an uploaded file. The
* value is obtained from the current module's controller configuration.
*
* @param mc The current module's configuration.
*
* @return The maximum allowable file size, in bytes.
*/
protected long getSizeMax(ModuleConfig mc) {
return convertSizeToBytes(
mc.getControllerConfig().getMaxFileSize(),
DEFAULT_SIZE_MAX);
}
/**
* Returns the size threshold which determines whether an uploaded file
* will be written to disk or cached in memory.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -