Browse Source
* commit '82821084232c874195df5b0bb014248b06a4d618': ant fix rm .. 添加apache/commons-fileupload10.0
superman
7 years ago
30 changed files with 5742 additions and 2 deletions
@ -0,0 +1,80 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.disk.DiskFileItem; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> The default implementation of the |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.FileItem FileItem} interface. |
||||||
|
* |
||||||
|
* <p> After retrieving an instance of this class from a {@link |
||||||
|
* com.fr.third.org.apache.commons.fileupload.DiskFileUpload DiskFileUpload} instance (see |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.DiskFileUpload |
||||||
|
* #parseRequest(javax.servlet.http.HttpServletRequest)}), you may |
||||||
|
* either request all contents of file at once using {@link #get()} or |
||||||
|
* request an {@link java.io.InputStream InputStream} with |
||||||
|
* {@link #getInputStream()} and process the file without attempting to load |
||||||
|
* it into memory, which may come handy with large files. |
||||||
|
* |
||||||
|
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> |
||||||
|
* @author <a href="mailto:sean@informage.net">Sean Legassick</a> |
||||||
|
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
||||||
|
* @author <a href="mailto:jmcnally@apache.org">John McNally</a> |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* @author Sean C. Sullivan |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItem</code> instead. |
||||||
|
*/ |
||||||
|
public class DefaultFileItem |
||||||
|
extends DiskFileItem { |
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a new <code>DefaultFileItem</code> instance. |
||||||
|
* |
||||||
|
* @param fieldName The name of the form field. |
||||||
|
* @param contentType The content type passed by the browser or |
||||||
|
* <code>null</code> if not specified. |
||||||
|
* @param isFormField Whether or not this item is a plain form field, as |
||||||
|
* opposed to a file upload. |
||||||
|
* @param fileName The original filename in the user's filesystem, or |
||||||
|
* <code>null</code> if not specified. |
||||||
|
* @param sizeThreshold The threshold, in bytes, below which items will be |
||||||
|
* retained in memory and above which they will be |
||||||
|
* stored as a file. |
||||||
|
* @param repository The data repository, which is the directory in |
||||||
|
* which files will be created, should the item size |
||||||
|
* exceed the threshold. |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItem</code> instead. |
||||||
|
*/ |
||||||
|
public DefaultFileItem(String fieldName, String contentType, |
||||||
|
boolean isFormField, String fileName, int sizeThreshold, |
||||||
|
File repository) { |
||||||
|
super(fieldName, contentType, isFormField, fileName, sizeThreshold, |
||||||
|
repository); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,107 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.disk.DiskFileItemFactory; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p>The default {@link com.fr.third.org.apache.commons.fileupload.FileItemFactory} |
||||||
|
* implementation. This implementation creates |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.FileItem} instances which keep their |
||||||
|
* content either in memory, for smaller items, or in a temporary file on disk, |
||||||
|
* for larger items. The size threshold, above which content will be stored on |
||||||
|
* disk, is configurable, as is the directory in which temporary files will be |
||||||
|
* created.</p> |
||||||
|
* |
||||||
|
* <p>If not otherwise configured, the default configuration values are as |
||||||
|
* follows: |
||||||
|
* <ul> |
||||||
|
* <li>Size threshold is 10KB.</li> |
||||||
|
* <li>Repository is the system default temp directory, as returned by |
||||||
|
* <code>System.getProperty("java.io.tmpdir")</code>.</li> |
||||||
|
* </ul> |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public class DefaultFileItemFactory extends DiskFileItemFactory { |
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an unconfigured instance of this class. The resulting factory |
||||||
|
* may be configured by calling the appropriate setter methods. |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public DefaultFileItemFactory() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a preconfigured instance of this class. |
||||||
|
* |
||||||
|
* @param sizeThreshold The threshold, in bytes, below which items will be |
||||||
|
* retained in memory and above which they will be |
||||||
|
* stored as a file. |
||||||
|
* @param repository The data repository, which is the directory in |
||||||
|
* which files will be created, should the item size |
||||||
|
* exceed the threshold. |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public DefaultFileItemFactory(int sizeThreshold, File repository) { |
||||||
|
super(sizeThreshold, repository); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public Methods
|
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new {@link com.fr.third.org.apache.commons.fileupload.DefaultFileItem} |
||||||
|
* instance from the supplied parameters and the local factory |
||||||
|
* configuration. |
||||||
|
* |
||||||
|
* @param fieldName The name of the form field. |
||||||
|
* @param contentType The content type of the form field. |
||||||
|
* @param isFormField <code>true</code> if this is a plain form field; |
||||||
|
* <code>false</code> otherwise. |
||||||
|
* @param fileName The name of the uploaded file, if any, as supplied |
||||||
|
* by the browser or other client. |
||||||
|
* |
||||||
|
* @return The newly created file item. |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public FileItem createItem( |
||||||
|
String fieldName, |
||||||
|
String contentType, |
||||||
|
boolean isFormField, |
||||||
|
String fileName |
||||||
|
) { |
||||||
|
return new DefaultFileItem(fieldName, contentType, |
||||||
|
isFormField, fileName, getSizeThreshold(), getRepository()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,212 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.util.List; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
|
||||||
|
/** |
||||||
|
* <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 |
||||||
|
* com.fr.third.org.apache.commons.fileupload.FileItem}s associated with a given HTML |
||||||
|
* widget.</p> |
||||||
|
* |
||||||
|
* <p>Individual parts will be stored in temporary disk storage or in memory, |
||||||
|
* depending on their size, and will be available as {@link |
||||||
|
* com.fr.third.org.apache.commons.fileupload.FileItem}s.</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$ |
||||||
|
* |
||||||
|
* @deprecated Use <code>ServletFileUpload</code> together with |
||||||
|
* <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public class DiskFileUpload |
||||||
|
extends FileUploadBase { |
||||||
|
|
||||||
|
// ----------------------------------------------------------- Data members
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The factory to use to create new form items. |
||||||
|
*/ |
||||||
|
private DefaultFileItemFactory fileItemFactory; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an instance of this class which uses the default factory to |
||||||
|
* create <code>FileItem</code> instances. |
||||||
|
* |
||||||
|
* @see #DiskFileUpload(DefaultFileItemFactory fileItemFactory) |
||||||
|
* |
||||||
|
* @deprecated Use <code>FileUpload</code> instead. |
||||||
|
*/ |
||||||
|
public DiskFileUpload() { |
||||||
|
super(); |
||||||
|
this.fileItemFactory = new DefaultFileItemFactory(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an instance of this class which uses the supplied factory to |
||||||
|
* create <code>FileItem</code> instances. |
||||||
|
* |
||||||
|
* @see #DiskFileUpload() |
||||||
|
* @param fileItemFactory The file item factory to use. |
||||||
|
* |
||||||
|
* @deprecated Use <code>FileUpload</code> instead. |
||||||
|
*/ |
||||||
|
public DiskFileUpload(DefaultFileItemFactory fileItemFactory) { |
||||||
|
super(); |
||||||
|
this.fileItemFactory = fileItemFactory; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------- Property accessors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the factory class used when creating file items. |
||||||
|
* |
||||||
|
* @return The factory class for new file items. |
||||||
|
* |
||||||
|
* @deprecated Use <code>FileUpload</code> instead. |
||||||
|
*/ |
||||||
|
public FileItemFactory getFileItemFactory() { |
||||||
|
return fileItemFactory; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the factory class to use when creating file items. The factory must |
||||||
|
* be an instance of <code>DefaultFileItemFactory</code> or a subclass |
||||||
|
* thereof, or else a <code>ClassCastException</code> will be thrown. |
||||||
|
* |
||||||
|
* @param factory The factory class for new file items. |
||||||
|
* |
||||||
|
* @deprecated Use <code>FileUpload</code> instead. |
||||||
|
*/ |
||||||
|
public void setFileItemFactory(FileItemFactory factory) { |
||||||
|
this.fileItemFactory = (DefaultFileItemFactory) factory; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the size threshold beyond which files are written directly to |
||||||
|
* disk. |
||||||
|
* |
||||||
|
* @return The size threshold, in bytes. |
||||||
|
* |
||||||
|
* @see #setSizeThreshold(int) |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public int getSizeThreshold() { |
||||||
|
return fileItemFactory.getSizeThreshold(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the size threshold beyond which files are written directly to disk. |
||||||
|
* |
||||||
|
* @param sizeThreshold The size threshold, in bytes. |
||||||
|
* |
||||||
|
* @see #getSizeThreshold() |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public void setSizeThreshold(int sizeThreshold) { |
||||||
|
fileItemFactory.setSizeThreshold(sizeThreshold); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the location used to temporarily store files that are larger |
||||||
|
* than the configured size threshold. |
||||||
|
* |
||||||
|
* @return The path to the temporary file location. |
||||||
|
* |
||||||
|
* @see #setRepositoryPath(String) |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public String getRepositoryPath() { |
||||||
|
return fileItemFactory.getRepository().getPath(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the location used to temporarily store files that are larger |
||||||
|
* than the configured size threshold. |
||||||
|
* |
||||||
|
* @param repositoryPath The path to the temporary file location. |
||||||
|
* |
||||||
|
* @see #getRepositoryPath() |
||||||
|
* |
||||||
|
* @deprecated Use <code>DiskFileItemFactory</code> instead. |
||||||
|
*/ |
||||||
|
public void setRepositoryPath(String repositoryPath) { |
||||||
|
fileItemFactory.setRepository(new File(repositoryPath)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> |
||||||
|
* compliant <code>multipart/form-data</code> stream. If files are stored |
||||||
|
* on disk, the path is given by <code>getRepository()</code>. |
||||||
|
* |
||||||
|
* @param req The servlet request to be parsed. Must be non-null. |
||||||
|
* @param sizeThreshold The max size in bytes to be stored in memory. |
||||||
|
* @param sizeMax The maximum allowed upload size, in bytes. |
||||||
|
* @param path The location where the files should be stored. |
||||||
|
* |
||||||
|
* @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 <code>ServletFileUpload</code> instead. |
||||||
|
*/ |
||||||
|
public List /* FileItem */ parseRequest(HttpServletRequest req, |
||||||
|
int sizeThreshold, |
||||||
|
long sizeMax, String path) |
||||||
|
throws FileUploadException { |
||||||
|
setSizeThreshold(sizeThreshold); |
||||||
|
setSizeMax(sizeMax); |
||||||
|
setRepositoryPath(path); |
||||||
|
return parseRequest(req); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,226 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.OutputStream; |
||||||
|
import java.io.Serializable; |
||||||
|
import java.io.UnsupportedEncodingException; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p> This class represents a file or form item that was received within a |
||||||
|
* <code>multipart/form-data</code> POST request. |
||||||
|
* |
||||||
|
* <p> After retrieving an instance of this class from a {@link |
||||||
|
* com.fr.third.org.apache.commons.fileupload.FileUpload FileUpload} instance (see |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.FileUpload |
||||||
|
* #parseRequest(javax.servlet.http.HttpServletRequest)}), you may |
||||||
|
* either request all contents of the file at once using {@link #get()} or |
||||||
|
* request an {@link java.io.InputStream InputStream} with |
||||||
|
* {@link #getInputStream()} and process the file without attempting to load |
||||||
|
* it into memory, which may come handy with large files. |
||||||
|
* |
||||||
|
* <p> While this interface does not extend |
||||||
|
* <code>javax.activation.DataSource</code> per se (to avoid a seldom used |
||||||
|
* dependency), several of the defined methods are specifically defined with |
||||||
|
* the same signatures as methods in that interface. This allows an |
||||||
|
* implementation of this interface to also implement |
||||||
|
* <code>javax.activation.DataSource</code> with minimal additional work. |
||||||
|
* |
||||||
|
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> |
||||||
|
* @author <a href="mailto:sean@informage.net">Sean Legassick</a> |
||||||
|
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public interface FileItem |
||||||
|
extends Serializable { |
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------- Methods from javax.activation.DataSource
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns an {@link java.io.InputStream InputStream} that can be |
||||||
|
* used to retrieve the contents of the file. |
||||||
|
* |
||||||
|
* @return An {@link java.io.InputStream InputStream} that can be |
||||||
|
* used to retrieve the contents of the file. |
||||||
|
* |
||||||
|
* @throws IOException if an error occurs. |
||||||
|
*/ |
||||||
|
InputStream getInputStream() |
||||||
|
throws IOException; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the content type passed by the browser or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
* |
||||||
|
* @return The content type passed by the browser or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
*/ |
||||||
|
String getContentType(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the original filename in the client's filesystem, as provided by |
||||||
|
* the browser (or other client software). In most cases, this will be the |
||||||
|
* base file name, without path information. However, some clients, such as |
||||||
|
* the Opera browser, do include path information. |
||||||
|
* |
||||||
|
* @return The original filename in the client's filesystem. |
||||||
|
*/ |
||||||
|
String getName(); |
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------- FileItem methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a hint as to whether or not the file contents will be read |
||||||
|
* from memory. |
||||||
|
* |
||||||
|
* @return <code>true</code> if the file contents will be read from memory; |
||||||
|
* <code>false</code> otherwise. |
||||||
|
*/ |
||||||
|
boolean isInMemory(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the size of the file item. |
||||||
|
* |
||||||
|
* @return The size of the file item, in bytes. |
||||||
|
*/ |
||||||
|
long getSize(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the contents of the file item as an array of bytes. |
||||||
|
* |
||||||
|
* @return The contents of the file item as an array of bytes. |
||||||
|
*/ |
||||||
|
byte[] get(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the contents of the file item as a String, using the specified |
||||||
|
* encoding. This method uses {@link #get()} to retrieve the |
||||||
|
* contents of the item. |
||||||
|
* |
||||||
|
* @param encoding The character encoding to use. |
||||||
|
* |
||||||
|
* @return The contents of the item, as a string. |
||||||
|
* |
||||||
|
* @throws UnsupportedEncodingException if the requested character |
||||||
|
* encoding is not available. |
||||||
|
*/ |
||||||
|
String getString(String encoding) |
||||||
|
throws UnsupportedEncodingException; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the contents of the file item as a String, using the default |
||||||
|
* character encoding. This method uses {@link #get()} to retrieve the |
||||||
|
* contents of the item. |
||||||
|
* |
||||||
|
* @return The contents of the item, as a string. |
||||||
|
*/ |
||||||
|
String getString(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* A convenience method to write an uploaded item to disk. The client code |
||||||
|
* is not concerned with whether or not the item is stored in memory, or on |
||||||
|
* disk in a temporary location. They just want to write the uploaded item |
||||||
|
* to a file. |
||||||
|
* <p> |
||||||
|
* This method is not guaranteed to succeed if called more than once for |
||||||
|
* the same item. This allows a particular implementation to use, for |
||||||
|
* example, file renaming, where possible, rather than copying all of the |
||||||
|
* underlying data, thus gaining a significant performance benefit. |
||||||
|
* |
||||||
|
* @param file The <code>File</code> into which the uploaded item should |
||||||
|
* be stored. |
||||||
|
* |
||||||
|
* @throws Exception if an error occurs. |
||||||
|
*/ |
||||||
|
void write(File file) throws Exception; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Deletes the underlying storage for a file item, including deleting any |
||||||
|
* associated temporary disk file. Although this storage will be deleted |
||||||
|
* automatically when the <code>FileItem</code> instance is garbage |
||||||
|
* collected, this method can be used to ensure that this is done at an |
||||||
|
* earlier time, thus preserving system resources. |
||||||
|
*/ |
||||||
|
void delete(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the name of the field in the multipart form corresponding to |
||||||
|
* this file item. |
||||||
|
* |
||||||
|
* @return The name of the form field. |
||||||
|
*/ |
||||||
|
String getFieldName(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the field name used to reference this file item. |
||||||
|
* |
||||||
|
* @param name The name of the form field. |
||||||
|
*/ |
||||||
|
void setFieldName(String name); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Determines whether or not a <code>FileItem</code> instance represents |
||||||
|
* a simple form field. |
||||||
|
* |
||||||
|
* @return <code>true</code> if the instance represents a simple form |
||||||
|
* field; <code>false</code> if it represents an uploaded file. |
||||||
|
*/ |
||||||
|
boolean isFormField(); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Specifies whether or not a <code>FileItem</code> instance represents |
||||||
|
* a simple form field. |
||||||
|
* |
||||||
|
* @param state <code>true</code> if the instance represents a simple form |
||||||
|
* field; <code>false</code> if it represents an uploaded file. |
||||||
|
*/ |
||||||
|
void setFormField(boolean state); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns an {@link java.io.OutputStream OutputStream} that can |
||||||
|
* be used for storing the contents of the file. |
||||||
|
* |
||||||
|
* @return An {@link java.io.OutputStream OutputStream} that can be used |
||||||
|
* for storing the contensts of the file. |
||||||
|
* |
||||||
|
* @throws IOException if an error occurs. |
||||||
|
*/ |
||||||
|
OutputStream getOutputStream() throws IOException; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p>A factory interface for creating {@link FileItem} instances. Factories |
||||||
|
* can provide their own custom configuration, over and above that provided |
||||||
|
* by the default file upload implementation.</p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public interface FileItemFactory { |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new {@link FileItem} instance from the supplied parameters and |
||||||
|
* any local factory configuration. |
||||||
|
* |
||||||
|
* @param fieldName The name of the form field. |
||||||
|
* @param contentType The content type of the form field. |
||||||
|
* @param isFormField <code>true</code> if this is a plain form field; |
||||||
|
* <code>false</code> otherwise. |
||||||
|
* @param fileName The name of the uploaded file, if any, as supplied |
||||||
|
* by the browser or other client. |
||||||
|
* |
||||||
|
* @return The newly created file item. |
||||||
|
*/ |
||||||
|
FileItem createItem( |
||||||
|
String fieldName, |
||||||
|
String contentType, |
||||||
|
boolean isFormField, |
||||||
|
String fileName |
||||||
|
); |
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* An iterator, as returned by |
||||||
|
* {@link FileUploadBase#getItemIterator(RequestContext)}. |
||||||
|
*/ |
||||||
|
public interface FileItemIterator { |
||||||
|
/** |
||||||
|
* Returns, whether another instance of {@link FileItemStream} |
||||||
|
* is available. |
||||||
|
* @throws FileUploadException Parsing or processing the |
||||||
|
* file item failed. |
||||||
|
* @throws IOException Reading the file item failed. |
||||||
|
* @return True, if one or more additional file items |
||||||
|
* are available, otherwise false. |
||||||
|
*/ |
||||||
|
boolean hasNext() throws FileUploadException, IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the next available {@link FileItemStream}. |
||||||
|
* @throws java.util.NoSuchElementException No more items are available. Use |
||||||
|
* {@link #hasNext()} to prevent this exception. |
||||||
|
* @throws FileUploadException Parsing or processing the |
||||||
|
* file item failed. |
||||||
|
* @throws IOException Reading the file item failed. |
||||||
|
* @return FileItemStream instance, which provides |
||||||
|
* access to the next file item. |
||||||
|
*/ |
||||||
|
FileItemStream next() throws FileUploadException, IOException; |
||||||
|
} |
@ -0,0 +1,97 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> This interface provides access to a file or form item that was |
||||||
|
* received within a <code>multipart/form-data</code> POST request. |
||||||
|
* The items contents are retrieved by calling {@link #openStream()}.</p> |
||||||
|
* <p>Instances of this class are created by accessing the |
||||||
|
* iterator, returned by |
||||||
|
* {@link FileUploadBase#getItemIterator(RequestContext)}.</p> |
||||||
|
* <p><em>Note</em>: There is an interaction between the iterator and |
||||||
|
* its associated instances of {@link FileItemStream}: By invoking |
||||||
|
* {@link java.util.Iterator#hasNext()} on the iterator, you discard all data, |
||||||
|
* which hasn't been read so far from the previous data.</p> |
||||||
|
*/ |
||||||
|
public interface FileItemStream { |
||||||
|
/** |
||||||
|
* This exception is thrown, if an attempt is made to read |
||||||
|
* data from the {@link InputStream}, which has been returned |
||||||
|
* by {@link FileItemStream#openStream()}, after |
||||||
|
* {@link java.util.Iterator#hasNext()} has been invoked on the |
||||||
|
* iterator, which created the {@link FileItemStream}. |
||||||
|
*/ |
||||||
|
public static class ItemSkippedException extends IOException { |
||||||
|
/** |
||||||
|
* The exceptions serial version UID, which is being used |
||||||
|
* when serializing an exception instance. |
||||||
|
*/ |
||||||
|
private static final long serialVersionUID = -7280778431581963740L; |
||||||
|
} |
||||||
|
|
||||||
|
/** Creates an {@link InputStream}, which allows to read the |
||||||
|
* items contents. |
||||||
|
* @return The input stream, from which the items data may |
||||||
|
* be read. |
||||||
|
* @throws IllegalStateException The method was already invoked on |
||||||
|
* this item. It is not possible to recreate the data stream. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
* @see ItemSkippedException |
||||||
|
*/ |
||||||
|
InputStream openStream() throws IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the content type passed by the browser or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
* |
||||||
|
* @return The content type passed by the browser or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
*/ |
||||||
|
String getContentType(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the original filename in the client's filesystem, as provided by |
||||||
|
* the browser (or other client software). In most cases, this will be the |
||||||
|
* base file name, without path information. However, some clients, such as |
||||||
|
* the Opera browser, do include path information. |
||||||
|
* |
||||||
|
* @return The original filename in the client's filesystem. |
||||||
|
*/ |
||||||
|
String getName(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the name of the field in the multipart form corresponding to |
||||||
|
* this file item. |
||||||
|
* |
||||||
|
* @return The name of the form field. |
||||||
|
*/ |
||||||
|
String getFieldName(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Determines whether or not a <code>FileItem</code> instance represents |
||||||
|
* a simple form field. |
||||||
|
* |
||||||
|
* @return <code>true</code> if the instance represents a simple form |
||||||
|
* field; <code>false</code> if it represents an uploaded file. |
||||||
|
*/ |
||||||
|
boolean isFormField(); |
||||||
|
} |
@ -0,0 +1,106 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <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(javax.servlet.http.HttpServletRequest)} to acquire a list |
||||||
|
* of {@link com.fr.third.org.apache.commons.fileupload.FileItem FileItems} 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$ |
||||||
|
*/ |
||||||
|
public class FileUpload |
||||||
|
extends FileUploadBase { |
||||||
|
|
||||||
|
// ----------------------------------------------------------- Data members
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The factory to use to create new form items. |
||||||
|
*/ |
||||||
|
private FileItemFactory fileItemFactory; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an uninitialised instance of this class. A factory must be |
||||||
|
* configured, using <code>setFileItemFactory()</code>, before attempting |
||||||
|
* to parse requests. |
||||||
|
* |
||||||
|
* @see #FileUpload(FileItemFactory) |
||||||
|
*/ |
||||||
|
public FileUpload() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an instance of this class which uses the supplied factory to |
||||||
|
* create <code>FileItem</code> instances. |
||||||
|
* |
||||||
|
* @see #FileUpload() |
||||||
|
* @param fileItemFactory The factory to use for creating file items. |
||||||
|
*/ |
||||||
|
public FileUpload(FileItemFactory fileItemFactory) { |
||||||
|
super(); |
||||||
|
this.fileItemFactory = fileItemFactory; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------- Property accessors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the factory class used when creating file items. |
||||||
|
* |
||||||
|
* @return The factory class for new file items. |
||||||
|
*/ |
||||||
|
public FileItemFactory getFileItemFactory() { |
||||||
|
return fileItemFactory; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the factory class to use when creating file items. |
||||||
|
* |
||||||
|
* @param factory The factory class for new file items. |
||||||
|
*/ |
||||||
|
public void setFileItemFactory(FileItemFactory factory) { |
||||||
|
this.fileItemFactory = factory; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,95 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.io.PrintStream; |
||||||
|
import java.io.PrintWriter; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Exception for errors encountered while processing the request. |
||||||
|
* |
||||||
|
* @author <a href="mailto:jmcnally@collab.net">John McNally</a> |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public class FileUploadException extends Exception { |
||||||
|
/** |
||||||
|
* Serial version UID, being used, if the exception |
||||||
|
* is serialized. |
||||||
|
*/ |
||||||
|
private static final long serialVersionUID = 8881893724388807504L; |
||||||
|
/** |
||||||
|
* The exceptions cause. We overwrite the cause of |
||||||
|
* the super class, which isn't available in Java 1.3. |
||||||
|
*/ |
||||||
|
private final Throwable cause; |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a new <code>FileUploadException</code> without message. |
||||||
|
*/ |
||||||
|
public FileUploadException() { |
||||||
|
this(null, null); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a new <code>FileUploadException</code> with specified detail |
||||||
|
* message. |
||||||
|
* |
||||||
|
* @param msg the error message. |
||||||
|
*/ |
||||||
|
public FileUploadException(final String msg) { |
||||||
|
this(msg, null); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new <code>FileUploadException</code> with the given |
||||||
|
* detail message and cause. |
||||||
|
* @param msg The exceptions detail message. |
||||||
|
* @param cause The exceptions cause. |
||||||
|
*/ |
||||||
|
public FileUploadException(String msg, Throwable cause) { |
||||||
|
super(msg); |
||||||
|
this.cause = cause; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Prints this throwable and its backtrace to the specified print stream. |
||||||
|
* |
||||||
|
* @param stream <code>PrintStream</code> to use for output |
||||||
|
*/ |
||||||
|
public void printStackTrace(PrintStream stream) { |
||||||
|
super.printStackTrace(stream); |
||||||
|
if (cause != null) { |
||||||
|
stream.println("Caused by:"); |
||||||
|
cause.printStackTrace(stream); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Prints this throwable and its backtrace to the specified |
||||||
|
* print writer. |
||||||
|
* |
||||||
|
* @param writer <code>PrintWriter</code> to use for output |
||||||
|
*/ |
||||||
|
public void printStackTrace(PrintWriter writer) { |
||||||
|
super.printStackTrace(writer); |
||||||
|
if (cause != null) { |
||||||
|
writer.println("Caused by:"); |
||||||
|
cause.printStackTrace(writer); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,299 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* A simple parser intended to parse sequences of name/value pairs. |
||||||
|
* Parameter values are exptected to be enclosed in quotes if they |
||||||
|
* contain unsafe characters, such as '=' characters or separators. |
||||||
|
* Parameter values are optional and can be omitted. |
||||||
|
* |
||||||
|
* <p> |
||||||
|
* <code>param1 = value; param2 = "anything goes; really"; param3</code> |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> |
||||||
|
*/ |
||||||
|
|
||||||
|
public class ParameterParser { |
||||||
|
/** |
||||||
|
* String to be parsed. |
||||||
|
*/ |
||||||
|
private char[] chars = null; |
||||||
|
|
||||||
|
/** |
||||||
|
* Current position in the string. |
||||||
|
*/ |
||||||
|
private int pos = 0; |
||||||
|
|
||||||
|
/** |
||||||
|
* Maximum position in the string. |
||||||
|
*/ |
||||||
|
private int len = 0; |
||||||
|
|
||||||
|
/** |
||||||
|
* Start of a token. |
||||||
|
*/ |
||||||
|
private int i1 = 0; |
||||||
|
|
||||||
|
/** |
||||||
|
* End of a token. |
||||||
|
*/ |
||||||
|
private int i2 = 0; |
||||||
|
|
||||||
|
/** |
||||||
|
* Whether names stored in the map should be converted to lower case. |
||||||
|
*/ |
||||||
|
private boolean lowerCaseNames = false; |
||||||
|
|
||||||
|
/** |
||||||
|
* Default ParameterParser constructor. |
||||||
|
*/ |
||||||
|
public ParameterParser() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Are there any characters left to parse? |
||||||
|
* |
||||||
|
* @return <tt>true</tt> if there are unparsed characters, |
||||||
|
* <tt>false</tt> otherwise. |
||||||
|
*/ |
||||||
|
private boolean hasChar() { |
||||||
|
return this.pos < this.len; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* A helper method to process the parsed token. This method removes |
||||||
|
* leading and trailing blanks as well as enclosing quotation marks, |
||||||
|
* when necessary. |
||||||
|
* |
||||||
|
* @param quoted <tt>true</tt> if quotation marks are expected, |
||||||
|
* <tt>false</tt> otherwise. |
||||||
|
* @return the token |
||||||
|
*/ |
||||||
|
private String getToken(boolean quoted) { |
||||||
|
// Trim leading white spaces
|
||||||
|
while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) { |
||||||
|
i1++; |
||||||
|
} |
||||||
|
// Trim trailing white spaces
|
||||||
|
while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) { |
||||||
|
i2--; |
||||||
|
} |
||||||
|
// Strip away quotation marks if necessary
|
||||||
|
if (quoted) { |
||||||
|
if (((i2 - i1) >= 2) |
||||||
|
&& (chars[i1] == '"') |
||||||
|
&& (chars[i2 - 1] == '"')) { |
||||||
|
i1++; |
||||||
|
i2--; |
||||||
|
} |
||||||
|
} |
||||||
|
String result = null; |
||||||
|
if (i2 > i1) { |
||||||
|
result = new String(chars, i1, i2 - i1); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests if the given character is present in the array of characters. |
||||||
|
* |
||||||
|
* @param ch the character to test for presense in the array of characters |
||||||
|
* @param charray the array of characters to test against |
||||||
|
* |
||||||
|
* @return <tt>true</tt> if the character is present in the array of |
||||||
|
* characters, <tt>false</tt> otherwise. |
||||||
|
*/ |
||||||
|
private boolean isOneOf(char ch, final char[] charray) { |
||||||
|
boolean result = false; |
||||||
|
for (int i = 0; i < charray.length; i++) { |
||||||
|
if (ch == charray[i]) { |
||||||
|
result = true; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Parses out a token until any of the given terminators |
||||||
|
* is encountered. |
||||||
|
* |
||||||
|
* @param terminators the array of terminating characters. Any of these |
||||||
|
* characters when encountered signify the end of the token |
||||||
|
* |
||||||
|
* @return the token |
||||||
|
*/ |
||||||
|
private String parseToken(final char[] terminators) { |
||||||
|
char ch; |
||||||
|
i1 = pos; |
||||||
|
i2 = pos; |
||||||
|
while (hasChar()) { |
||||||
|
ch = chars[pos]; |
||||||
|
if (isOneOf(ch, terminators)) { |
||||||
|
break; |
||||||
|
} |
||||||
|
i2++; |
||||||
|
pos++; |
||||||
|
} |
||||||
|
return getToken(false); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Parses out a token until any of the given terminators |
||||||
|
* is encountered outside the quotation marks. |
||||||
|
* |
||||||
|
* @param terminators the array of terminating characters. Any of these |
||||||
|
* characters when encountered outside the quotation marks signify the end |
||||||
|
* of the token |
||||||
|
* |
||||||
|
* @return the token |
||||||
|
*/ |
||||||
|
private String parseQuotedToken(final char[] terminators) { |
||||||
|
char ch; |
||||||
|
i1 = pos; |
||||||
|
i2 = pos; |
||||||
|
boolean quoted = false; |
||||||
|
boolean charEscaped = false; |
||||||
|
while (hasChar()) { |
||||||
|
ch = chars[pos]; |
||||||
|
if (!quoted && isOneOf(ch, terminators)) { |
||||||
|
break; |
||||||
|
} |
||||||
|
if (!charEscaped && ch == '"') { |
||||||
|
quoted = !quoted; |
||||||
|
} |
||||||
|
charEscaped = (!charEscaped && ch == '\\'); |
||||||
|
i2++; |
||||||
|
pos++; |
||||||
|
|
||||||
|
} |
||||||
|
return getToken(true); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns <tt>true</tt> if parameter names are to be converted to lower |
||||||
|
* case when name/value pairs are parsed. |
||||||
|
* |
||||||
|
* @return <tt>true</tt> if parameter names are to be |
||||||
|
* converted to lower case when name/value pairs are parsed. |
||||||
|
* Otherwise returns <tt>false</tt> |
||||||
|
*/ |
||||||
|
public boolean isLowerCaseNames() { |
||||||
|
return this.lowerCaseNames; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the flag if parameter names are to be converted to lower case when |
||||||
|
* name/value pairs are parsed. |
||||||
|
* |
||||||
|
* @param b <tt>true</tt> if parameter names are to be |
||||||
|
* converted to lower case when name/value pairs are parsed. |
||||||
|
* <tt>false</tt> otherwise. |
||||||
|
*/ |
||||||
|
public void setLowerCaseNames(boolean b) { |
||||||
|
this.lowerCaseNames = b; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Extracts a map of name/value pairs from the given string. Names are |
||||||
|
* expected to be unique. |
||||||
|
* |
||||||
|
* @param str the string that contains a sequence of name/value pairs |
||||||
|
* @param separator the name/value pairs separator |
||||||
|
* |
||||||
|
* @return a map of name/value pairs |
||||||
|
*/ |
||||||
|
public Map parse(final String str, char separator) { |
||||||
|
if (str == null) { |
||||||
|
return new HashMap(); |
||||||
|
} |
||||||
|
return parse(str.toCharArray(), separator); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Extracts a map of name/value pairs from the given array of |
||||||
|
* characters. Names are expected to be unique. |
||||||
|
* |
||||||
|
* @param chars the array of characters that contains a sequence of |
||||||
|
* name/value pairs |
||||||
|
* @param separator the name/value pairs separator |
||||||
|
* |
||||||
|
* @return a map of name/value pairs |
||||||
|
*/ |
||||||
|
public Map parse(final char[] chars, char separator) { |
||||||
|
if (chars == null) { |
||||||
|
return new HashMap(); |
||||||
|
} |
||||||
|
return parse(chars, 0, chars.length, separator); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Extracts a map of name/value pairs from the given array of |
||||||
|
* characters. Names are expected to be unique. |
||||||
|
* |
||||||
|
* @param chars the array of characters that contains a sequence of |
||||||
|
* name/value pairs |
||||||
|
* @param offset - the initial offset. |
||||||
|
* @param length - the length. |
||||||
|
* @param separator the name/value pairs separator |
||||||
|
* |
||||||
|
* @return a map of name/value pairs |
||||||
|
*/ |
||||||
|
public Map parse( |
||||||
|
final char[] chars, |
||||||
|
int offset, |
||||||
|
int length, |
||||||
|
char separator) { |
||||||
|
|
||||||
|
if (chars == null) { |
||||||
|
return new HashMap(); |
||||||
|
} |
||||||
|
HashMap params = new HashMap(); |
||||||
|
this.chars = chars; |
||||||
|
this.pos = offset; |
||||||
|
this.len = length; |
||||||
|
|
||||||
|
String paramName = null; |
||||||
|
String paramValue = null; |
||||||
|
while (hasChar()) { |
||||||
|
paramName = parseToken(new char[] { |
||||||
|
'=', separator }); |
||||||
|
paramValue = null; |
||||||
|
if (hasChar() && (chars[pos] == '=')) { |
||||||
|
pos++; // skip '='
|
||||||
|
paramValue = parseQuotedToken(new char[] { |
||||||
|
separator }); |
||||||
|
} |
||||||
|
if (hasChar() && (chars[pos] == separator)) { |
||||||
|
pos++; // skip separator
|
||||||
|
} |
||||||
|
if ((paramName != null) && (paramName.length() > 0)) { |
||||||
|
if (this.lowerCaseNames) { |
||||||
|
paramName = paramName.toLowerCase(); |
||||||
|
} |
||||||
|
params.put(paramName, paramValue); |
||||||
|
} |
||||||
|
} |
||||||
|
return params; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The {@link ProgressListener} may be used to display a progress bar |
||||||
|
* or do stuff like that. |
||||||
|
*/ |
||||||
|
public interface ProgressListener { |
||||||
|
/** Updates the listeners status information. |
||||||
|
* @param pBytesRead The total number of bytes, which have been read |
||||||
|
* so far. |
||||||
|
* @param pContentLength The total number of bytes, which are being |
||||||
|
* read. May be -1, if this number is unknown. |
||||||
|
* @param pItems The number of the field, which is currently being |
||||||
|
* read. (0 = no item so far, 1 = first item is being read, ...) |
||||||
|
*/ |
||||||
|
void update(long pBytesRead, long pContentLength, int pItems); |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload; |
||||||
|
|
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p>Abstracts access to the request information needed for file uploads. This |
||||||
|
* interfsace should be implemented for each type of request that may be |
||||||
|
* handled by FileUpload, such as servlets and portlets.</p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* |
||||||
|
* @since FileUpload 1.1 |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public interface RequestContext { |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the character encoding for the request. |
||||||
|
* |
||||||
|
* @return The character encoding for the request. |
||||||
|
*/ |
||||||
|
String getCharacterEncoding(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the content type of the request. |
||||||
|
* |
||||||
|
* @return The content type of the request. |
||||||
|
*/ |
||||||
|
String getContentType(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the content length of the request. |
||||||
|
* |
||||||
|
* @return The content length of the request. |
||||||
|
*/ |
||||||
|
int getContentLength(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the input stream for the request. |
||||||
|
* |
||||||
|
* @return The input stream for the request. |
||||||
|
* |
||||||
|
* @throws IOException if a problem occurs. |
||||||
|
*/ |
||||||
|
InputStream getInputStream() throws IOException; |
||||||
|
} |
@ -0,0 +1,702 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.disk; |
||||||
|
|
||||||
|
import java.io.BufferedInputStream; |
||||||
|
import java.io.BufferedOutputStream; |
||||||
|
import java.io.ByteArrayInputStream; |
||||||
|
import java.io.File; |
||||||
|
import java.io.FileInputStream; |
||||||
|
import java.io.FileOutputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.OutputStream; |
||||||
|
import java.io.ObjectOutputStream; |
||||||
|
import java.io.ObjectInputStream; |
||||||
|
import java.io.UnsupportedEncodingException; |
||||||
|
import java.util.Map; |
||||||
|
import com.fr.third.org.apache.commons.io.IOUtils; |
||||||
|
import com.fr.third.org.apache.commons.io.FileCleaner; |
||||||
|
import com.fr.third.org.apache.commons.io.output.DeferredFileOutputStream; |
||||||
|
|
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileItem; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileUploadException; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.ParameterParser; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* <p> The default implementation of the |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.FileItem FileItem} interface. |
||||||
|
* |
||||||
|
* <p> After retrieving an instance of this class from a {@link |
||||||
|
* com.fr.third.org.apache.commons.fileupload.DiskFileUpload DiskFileUpload} instance (see |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.DiskFileUpload |
||||||
|
* #parseRequest(javax.servlet.http.HttpServletRequest)}), you may |
||||||
|
* either request all contents of file at once using {@link #get()} or |
||||||
|
* request an {@link java.io.InputStream InputStream} with |
||||||
|
* {@link #getInputStream()} and process the file without attempting to load |
||||||
|
* it into memory, which may come handy with large files. |
||||||
|
* |
||||||
|
* <p>When using the <code>DiskFileItemFactory</code>, then you should |
||||||
|
* consider the following: Temporary files are automatically deleted as |
||||||
|
* soon as they are no longer needed. (More precisely, when the |
||||||
|
* corresponding instance of {@link java.io.File} is garbage collected.) |
||||||
|
* This is done by the so-called reaper thread, which is started |
||||||
|
* automatically when the class {@link FileCleaner} is loaded. |
||||||
|
* It might make sense to terminate that thread, for example, if |
||||||
|
* your web application ends. See the section on "Resource cleanup" |
||||||
|
* in the users guide of commons-fileupload.</p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> |
||||||
|
* @author <a href="mailto:sean@informage.net">Sean Legassick</a> |
||||||
|
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a> |
||||||
|
* @author <a href="mailto:jmcnally@apache.org">John McNally</a> |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* @author Sean C. Sullivan |
||||||
|
* |
||||||
|
* @since FileUpload 1.1 |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public class DiskFileItem |
||||||
|
implements FileItem { |
||||||
|
|
||||||
|
// ----------------------------------------------------- Manifest constants
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Default content charset to be used when no explicit charset |
||||||
|
* parameter is provided by the sender. Media subtypes of the |
||||||
|
* "text" type are defined to have a default charset value of |
||||||
|
* "ISO-8859-1" when received via HTTP. |
||||||
|
*/ |
||||||
|
public static final String DEFAULT_CHARSET = "ISO-8859-1"; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Data members
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* UID used in unique file name generation. |
||||||
|
*/ |
||||||
|
private static final String UID = |
||||||
|
new java.rmi.server.UID().toString() |
||||||
|
.replace(':', '_').replace('-', '_'); |
||||||
|
|
||||||
|
/** |
||||||
|
* Counter used in unique identifier generation. |
||||||
|
*/ |
||||||
|
private static int counter = 0; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The name of the form field as provided by the browser. |
||||||
|
*/ |
||||||
|
private String fieldName; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The content type passed by the browser, or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
*/ |
||||||
|
private String contentType; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Whether or not this item is a simple form field. |
||||||
|
*/ |
||||||
|
private boolean isFormField; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The original filename in the user's filesystem. |
||||||
|
*/ |
||||||
|
private String fileName; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The size of the item, in bytes. This is used to cache the size when a |
||||||
|
* file item is moved from its original location. |
||||||
|
*/ |
||||||
|
private long size = -1; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The threshold above which uploads will be stored on disk. |
||||||
|
*/ |
||||||
|
private int sizeThreshold; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The directory in which uploaded files will be stored, if stored on disk. |
||||||
|
*/ |
||||||
|
private File repository; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Cached contents of the file. |
||||||
|
*/ |
||||||
|
private byte[] cachedContent; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Output stream for this item. |
||||||
|
*/ |
||||||
|
private transient DeferredFileOutputStream dfos; |
||||||
|
|
||||||
|
/** |
||||||
|
* File to allow for serialization of the content of this item. |
||||||
|
*/ |
||||||
|
private File dfosFile; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a new <code>DiskFileItem</code> instance. |
||||||
|
* |
||||||
|
* @param fieldName The name of the form field. |
||||||
|
* @param contentType The content type passed by the browser or |
||||||
|
* <code>null</code> if not specified. |
||||||
|
* @param isFormField Whether or not this item is a plain form field, as |
||||||
|
* opposed to a file upload. |
||||||
|
* @param fileName The original filename in the user's filesystem, or |
||||||
|
* <code>null</code> if not specified. |
||||||
|
* @param sizeThreshold The threshold, in bytes, below which items will be |
||||||
|
* retained in memory and above which they will be |
||||||
|
* stored as a file. |
||||||
|
* @param repository The data repository, which is the directory in |
||||||
|
* which files will be created, should the item size |
||||||
|
* exceed the threshold. |
||||||
|
*/ |
||||||
|
public DiskFileItem(String fieldName, String contentType, |
||||||
|
boolean isFormField, String fileName, int sizeThreshold, |
||||||
|
File repository) { |
||||||
|
this.fieldName = fieldName; |
||||||
|
this.contentType = contentType; |
||||||
|
this.isFormField = isFormField; |
||||||
|
this.fileName = fileName; |
||||||
|
this.sizeThreshold = sizeThreshold; |
||||||
|
this.repository = repository; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------- Methods from javax.activation.DataSource
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns an {@link java.io.InputStream InputStream} that can be |
||||||
|
* used to retrieve the contents of the file. |
||||||
|
* |
||||||
|
* @return An {@link java.io.InputStream InputStream} that can be |
||||||
|
* used to retrieve the contents of the file. |
||||||
|
* |
||||||
|
* @throws IOException if an error occurs. |
||||||
|
*/ |
||||||
|
public InputStream getInputStream() |
||||||
|
throws IOException { |
||||||
|
if (!isInMemory()) { |
||||||
|
return new FileInputStream(dfos.getFile()); |
||||||
|
} |
||||||
|
|
||||||
|
if (cachedContent == null) { |
||||||
|
cachedContent = dfos.getData(); |
||||||
|
} |
||||||
|
return new ByteArrayInputStream(cachedContent); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the content type passed by the agent or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
* |
||||||
|
* @return The content type passed by the agent or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
*/ |
||||||
|
public String getContentType() { |
||||||
|
return contentType; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the content charset passed by the agent or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
* |
||||||
|
* @return The content charset passed by the agent or <code>null</code> if |
||||||
|
* not defined. |
||||||
|
*/ |
||||||
|
public String getCharSet() { |
||||||
|
ParameterParser parser = new ParameterParser(); |
||||||
|
parser.setLowerCaseNames(true); |
||||||
|
// Parameter parser can handle null input
|
||||||
|
Map params = parser.parse(getContentType(), ';'); |
||||||
|
return (String) params.get("charset"); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the original filename in the client's filesystem. |
||||||
|
* |
||||||
|
* @return The original filename in the client's filesystem. |
||||||
|
*/ |
||||||
|
public String getName() { |
||||||
|
return fileName; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------- FileItem methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Provides a hint as to whether or not the file contents will be read |
||||||
|
* from memory. |
||||||
|
* |
||||||
|
* @return <code>true</code> if the file contents will be read |
||||||
|
* from memory; <code>false</code> otherwise. |
||||||
|
*/ |
||||||
|
public boolean isInMemory() { |
||||||
|
if (cachedContent != null) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
return dfos.isInMemory(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the size of the file. |
||||||
|
* |
||||||
|
* @return The size of the file, in bytes. |
||||||
|
*/ |
||||||
|
public long getSize() { |
||||||
|
if (size >= 0) { |
||||||
|
return size; |
||||||
|
} else if (cachedContent != null) { |
||||||
|
return cachedContent.length; |
||||||
|
} else if (dfos.isInMemory()) { |
||||||
|
return dfos.getData().length; |
||||||
|
} else { |
||||||
|
return dfos.getFile().length(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the contents of the file as an array of bytes. If the |
||||||
|
* contents of the file were not yet cached in memory, they will be |
||||||
|
* loaded from the disk storage and cached. |
||||||
|
* |
||||||
|
* @return The contents of the file as an array of bytes. |
||||||
|
*/ |
||||||
|
public byte[] get() { |
||||||
|
if (isInMemory()) { |
||||||
|
if (cachedContent == null) { |
||||||
|
cachedContent = dfos.getData(); |
||||||
|
} |
||||||
|
return cachedContent; |
||||||
|
} |
||||||
|
|
||||||
|
byte[] fileData = new byte[(int) getSize()]; |
||||||
|
FileInputStream fis = null; |
||||||
|
|
||||||
|
try { |
||||||
|
fis = new FileInputStream(dfos.getFile()); |
||||||
|
fis.read(fileData); |
||||||
|
} catch (IOException e) { |
||||||
|
fileData = null; |
||||||
|
} finally { |
||||||
|
if (fis != null) { |
||||||
|
try { |
||||||
|
fis.close(); |
||||||
|
} catch (IOException e) { |
||||||
|
// ignore
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return fileData; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the contents of the file as a String, using the specified |
||||||
|
* encoding. This method uses {@link #get()} to retrieve the |
||||||
|
* contents of the file. |
||||||
|
* |
||||||
|
* @param charset The charset to use. |
||||||
|
* |
||||||
|
* @return The contents of the file, as a string. |
||||||
|
* |
||||||
|
* @throws UnsupportedEncodingException if the requested character |
||||||
|
* encoding is not available. |
||||||
|
*/ |
||||||
|
public String getString(final String charset) |
||||||
|
throws UnsupportedEncodingException { |
||||||
|
return new String(get(), charset); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the contents of the file as a String, using the default |
||||||
|
* character encoding. This method uses {@link #get()} to retrieve the |
||||||
|
* contents of the file. |
||||||
|
* |
||||||
|
* @return The contents of the file, as a string. |
||||||
|
* |
||||||
|
* @todo Consider making this method throw UnsupportedEncodingException. |
||||||
|
*/ |
||||||
|
public String getString() { |
||||||
|
byte[] rawdata = get(); |
||||||
|
String charset = getCharSet(); |
||||||
|
if (charset == null) { |
||||||
|
charset = DEFAULT_CHARSET; |
||||||
|
} |
||||||
|
try { |
||||||
|
return new String(rawdata, charset); |
||||||
|
} catch (UnsupportedEncodingException e) { |
||||||
|
return new String(rawdata); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* A convenience method to write an uploaded item to disk. The client code |
||||||
|
* is not concerned with whether or not the item is stored in memory, or on |
||||||
|
* disk in a temporary location. They just want to write the uploaded item |
||||||
|
* to a file. |
||||||
|
* <p> |
||||||
|
* This implementation first attempts to rename the uploaded item to the |
||||||
|
* specified destination file, if the item was originally written to disk. |
||||||
|
* Otherwise, the data will be copied to the specified file. |
||||||
|
* <p> |
||||||
|
* This method is only guaranteed to work <em>once</em>, the first time it |
||||||
|
* is invoked for a particular item. This is because, in the event that the |
||||||
|
* method renames a temporary file, that file will no longer be available |
||||||
|
* to copy or rename again at a later time. |
||||||
|
* |
||||||
|
* @param file The <code>File</code> into which the uploaded item should |
||||||
|
* be stored. |
||||||
|
* |
||||||
|
* @throws Exception if an error occurs. |
||||||
|
*/ |
||||||
|
public void write(File file) throws Exception { |
||||||
|
if (isInMemory()) { |
||||||
|
FileOutputStream fout = null; |
||||||
|
try { |
||||||
|
fout = new FileOutputStream(file); |
||||||
|
fout.write(get()); |
||||||
|
} finally { |
||||||
|
if (fout != null) { |
||||||
|
fout.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
File outputFile = getStoreLocation(); |
||||||
|
if (outputFile != null) { |
||||||
|
// Save the length of the file
|
||||||
|
size = outputFile.length(); |
||||||
|
/* |
||||||
|
* The uploaded file is being stored on disk |
||||||
|
* in a temporary location so move it to the |
||||||
|
* desired file. |
||||||
|
*/ |
||||||
|
if (!outputFile.renameTo(file)) { |
||||||
|
BufferedInputStream in = null; |
||||||
|
BufferedOutputStream out = null; |
||||||
|
try { |
||||||
|
in = new BufferedInputStream( |
||||||
|
new FileInputStream(outputFile)); |
||||||
|
out = new BufferedOutputStream( |
||||||
|
new FileOutputStream(file)); |
||||||
|
IOUtils.copy(in, out); |
||||||
|
} finally { |
||||||
|
if (in != null) { |
||||||
|
try { |
||||||
|
in.close(); |
||||||
|
} catch (IOException e) { |
||||||
|
// ignore
|
||||||
|
} |
||||||
|
} |
||||||
|
if (out != null) { |
||||||
|
try { |
||||||
|
out.close(); |
||||||
|
} catch (IOException e) { |
||||||
|
// ignore
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
/* |
||||||
|
* For whatever reason we cannot write the |
||||||
|
* file to disk. |
||||||
|
*/ |
||||||
|
throw new FileUploadException( |
||||||
|
"Cannot write uploaded file to disk!"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Deletes the underlying storage for a file item, including deleting any |
||||||
|
* associated temporary disk file. Although this storage will be deleted |
||||||
|
* automatically when the <code>FileItem</code> instance is garbage |
||||||
|
* collected, this method can be used to ensure that this is done at an |
||||||
|
* earlier time, thus preserving system resources. |
||||||
|
*/ |
||||||
|
public void delete() { |
||||||
|
cachedContent = null; |
||||||
|
File outputFile = getStoreLocation(); |
||||||
|
if (outputFile != null && outputFile.exists()) { |
||||||
|
outputFile.delete(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the name of the field in the multipart form corresponding to |
||||||
|
* this file item. |
||||||
|
* |
||||||
|
* @return The name of the form field. |
||||||
|
* |
||||||
|
* @see #setFieldName(java.lang.String) |
||||||
|
* |
||||||
|
*/ |
||||||
|
public String getFieldName() { |
||||||
|
return fieldName; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the field name used to reference this file item. |
||||||
|
* |
||||||
|
* @param fieldName The name of the form field. |
||||||
|
* |
||||||
|
* @see #getFieldName() |
||||||
|
* |
||||||
|
*/ |
||||||
|
public void setFieldName(String fieldName) { |
||||||
|
this.fieldName = fieldName; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Determines whether or not a <code>FileItem</code> instance represents |
||||||
|
* a simple form field. |
||||||
|
* |
||||||
|
* @return <code>true</code> if the instance represents a simple form |
||||||
|
* field; <code>false</code> if it represents an uploaded file. |
||||||
|
* |
||||||
|
* @see #setFormField(boolean) |
||||||
|
* |
||||||
|
*/ |
||||||
|
public boolean isFormField() { |
||||||
|
return isFormField; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Specifies whether or not a <code>FileItem</code> instance represents |
||||||
|
* a simple form field. |
||||||
|
* |
||||||
|
* @param state <code>true</code> if the instance represents a simple form |
||||||
|
* field; <code>false</code> if it represents an uploaded file. |
||||||
|
* |
||||||
|
* @see #isFormField() |
||||||
|
* |
||||||
|
*/ |
||||||
|
public void setFormField(boolean state) { |
||||||
|
isFormField = state; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns an {@link java.io.OutputStream OutputStream} that can |
||||||
|
* be used for storing the contents of the file. |
||||||
|
* |
||||||
|
* @return An {@link java.io.OutputStream OutputStream} that can be used |
||||||
|
* for storing the contensts of the file. |
||||||
|
* |
||||||
|
* @throws IOException if an error occurs. |
||||||
|
*/ |
||||||
|
public OutputStream getOutputStream() |
||||||
|
throws IOException { |
||||||
|
if (dfos == null) { |
||||||
|
File outputFile = getTempFile(); |
||||||
|
dfos = new DeferredFileOutputStream(sizeThreshold, outputFile); |
||||||
|
} |
||||||
|
return dfos; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the {@link java.io.File} object for the <code>FileItem</code>'s |
||||||
|
* data's temporary location on the disk. Note that for |
||||||
|
* <code>FileItem</code>s that have their data stored in memory, |
||||||
|
* this method will return <code>null</code>. When handling large |
||||||
|
* files, you can use {@link java.io.File#renameTo(java.io.File)} to |
||||||
|
* move the file to new location without copying the data, if the |
||||||
|
* source and destination locations reside within the same logical |
||||||
|
* volume. |
||||||
|
* |
||||||
|
* @return The data file, or <code>null</code> if the data is stored in |
||||||
|
* memory. |
||||||
|
*/ |
||||||
|
public File getStoreLocation() { |
||||||
|
return dfos.getFile(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------ Protected methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Removes the file contents from the temporary storage. |
||||||
|
*/ |
||||||
|
protected void finalize() { |
||||||
|
File outputFile = dfos.getFile(); |
||||||
|
|
||||||
|
if (outputFile != null && outputFile.exists()) { |
||||||
|
outputFile.delete(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Creates and returns a {@link java.io.File File} representing a uniquely |
||||||
|
* named temporary file in the configured repository path. The lifetime of |
||||||
|
* the file is tied to the lifetime of the <code>FileItem</code> instance; |
||||||
|
* the file will be deleted when the instance is garbage collected. |
||||||
|
* |
||||||
|
* @return The {@link java.io.File File} to be used for temporary storage. |
||||||
|
*/ |
||||||
|
protected File getTempFile() { |
||||||
|
File tempDir = repository; |
||||||
|
if (tempDir == null) { |
||||||
|
tempDir = new File(System.getProperty("java.io.tmpdir")); |
||||||
|
} |
||||||
|
|
||||||
|
String tempFileName = "upload_" + UID + "_" + getUniqueId() + ".tmp"; |
||||||
|
|
||||||
|
File f = new File(tempDir, tempFileName); |
||||||
|
FileCleaner.track(f, this); |
||||||
|
return f; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// -------------------------------------------------------- Private methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns an identifier that is unique within the class loader used to |
||||||
|
* load this class, but does not have random-like apearance. |
||||||
|
* |
||||||
|
* @return A String with the non-random looking instance identifier. |
||||||
|
*/ |
||||||
|
private static String getUniqueId() { |
||||||
|
final int limit = 100000000; |
||||||
|
int current; |
||||||
|
synchronized (DiskFileItem.class) { |
||||||
|
current = counter++; |
||||||
|
} |
||||||
|
String id = Integer.toString(current); |
||||||
|
|
||||||
|
// If you manage to get more than 100 million of ids, you'll
|
||||||
|
// start getting ids longer than 8 characters.
|
||||||
|
if (current < limit) { |
||||||
|
id = ("00000000" + id).substring(id.length()); |
||||||
|
} |
||||||
|
return id; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns a string representation of this object. |
||||||
|
* |
||||||
|
* @return a string representation of this object. |
||||||
|
*/ |
||||||
|
public String toString() { |
||||||
|
return "name=" + this.getName() |
||||||
|
+ ", StoreLocation=" |
||||||
|
+ String.valueOf(this.getStoreLocation()) |
||||||
|
+ ", size=" |
||||||
|
+ this.getSize() |
||||||
|
+ "bytes, " |
||||||
|
+ "isFormField=" + isFormField() |
||||||
|
+ ", FieldName=" |
||||||
|
+ this.getFieldName(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// -------------------------------------------------- Serialization methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Writes the state of this object during serialization. |
||||||
|
* |
||||||
|
* @param out The stream to which the state should be written. |
||||||
|
* |
||||||
|
* @throws IOException if an error occurs. |
||||||
|
*/ |
||||||
|
private void writeObject(ObjectOutputStream out) throws IOException { |
||||||
|
// Read the data
|
||||||
|
if (dfos.isInMemory()) { |
||||||
|
cachedContent = get(); |
||||||
|
} else { |
||||||
|
cachedContent = null; |
||||||
|
dfosFile = dfos.getFile(); |
||||||
|
} |
||||||
|
|
||||||
|
// write out values
|
||||||
|
out.defaultWriteObject(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Reads the state of this object during deserialization. |
||||||
|
* |
||||||
|
* @param in The stream from which the state should be read. |
||||||
|
* |
||||||
|
* @throws IOException if an error occurs. |
||||||
|
* @throws ClassNotFoundException if class cannot be found. |
||||||
|
*/ |
||||||
|
private void readObject(ObjectInputStream in) |
||||||
|
throws IOException, ClassNotFoundException { |
||||||
|
// read values
|
||||||
|
in.defaultReadObject(); |
||||||
|
|
||||||
|
OutputStream output = getOutputStream(); |
||||||
|
if (cachedContent != null) { |
||||||
|
output.write(cachedContent); |
||||||
|
} else { |
||||||
|
FileInputStream input = new FileInputStream(dfosFile); |
||||||
|
|
||||||
|
IOUtils.copy(input, output); |
||||||
|
dfosFile.delete(); |
||||||
|
dfosFile = null; |
||||||
|
} |
||||||
|
output.close(); |
||||||
|
|
||||||
|
cachedContent = null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,195 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.disk; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
|
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileItem; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileItemFactory; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p>The default {@link com.fr.third.org.apache.commons.fileupload.FileItemFactory} |
||||||
|
* implementation. This implementation creates |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.FileItem} instances which keep their |
||||||
|
* content either in memory, for smaller items, or in a temporary file on disk, |
||||||
|
* for larger items. The size threshold, above which content will be stored on |
||||||
|
* disk, is configurable, as is the directory in which temporary files will be |
||||||
|
* created.</p> |
||||||
|
* |
||||||
|
* <p>If not otherwise configured, the default configuration values are as |
||||||
|
* follows: |
||||||
|
* <ul> |
||||||
|
* <li>Size threshold is 10KB.</li> |
||||||
|
* <li>Repository is the system default temp directory, as returned by |
||||||
|
* <code>System.getProperty("java.io.tmpdir")</code>.</li> |
||||||
|
* </ul> |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* <p>When using the <code>DiskFileItemFactory</code>, then you should |
||||||
|
* consider the following: Temporary files are automatically deleted as |
||||||
|
* soon as they are no longer needed. (More precisely, when the |
||||||
|
* corresponding instance of {@link java.io.File} is garbage collected.) |
||||||
|
* This is done by the so-called reaper thread, which is started |
||||||
|
* automatically when the class {@link org.apache.commons.io.FileCleaner} |
||||||
|
* is loaded. It might make sense to terminate that thread, for example, |
||||||
|
* if your web application ends. See the section on "Resource cleanup" |
||||||
|
* in the users guide of commons-fileupload.</p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* |
||||||
|
* @since FileUpload 1.1 |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public class DiskFileItemFactory implements FileItemFactory { |
||||||
|
|
||||||
|
// ----------------------------------------------------- Manifest constants
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The default threshold above which uploads will be stored on disk. |
||||||
|
*/ |
||||||
|
public static final int DEFAULT_SIZE_THRESHOLD = 10240; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------- Instance Variables
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The directory in which uploaded files will be stored, if stored on disk. |
||||||
|
*/ |
||||||
|
private File repository; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* The threshold above which uploads will be stored on disk. |
||||||
|
*/ |
||||||
|
private int sizeThreshold = DEFAULT_SIZE_THRESHOLD; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an unconfigured instance of this class. The resulting factory |
||||||
|
* may be configured by calling the appropriate setter methods. |
||||||
|
*/ |
||||||
|
public DiskFileItemFactory() { |
||||||
|
// Does nothing.
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs a preconfigured instance of this class. |
||||||
|
* |
||||||
|
* @param sizeThreshold The threshold, in bytes, below which items will be |
||||||
|
* retained in memory and above which they will be |
||||||
|
* stored as a file. |
||||||
|
* @param repository The data repository, which is the directory in |
||||||
|
* which files will be created, should the item size |
||||||
|
* exceed the threshold. |
||||||
|
*/ |
||||||
|
public DiskFileItemFactory(int sizeThreshold, File repository) { |
||||||
|
this.sizeThreshold = sizeThreshold; |
||||||
|
this.repository = repository; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------------- Properties
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the directory used to temporarily store files that are larger |
||||||
|
* than the configured size threshold. |
||||||
|
* |
||||||
|
* @return The directory in which temporary files will be located. |
||||||
|
* |
||||||
|
* @see #setRepository(java.io.File) |
||||||
|
* |
||||||
|
*/ |
||||||
|
public File getRepository() { |
||||||
|
return repository; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the directory used to temporarily store files that are larger |
||||||
|
* than the configured size threshold. |
||||||
|
* |
||||||
|
* @param repository The directory in which temporary files will be located. |
||||||
|
* |
||||||
|
* @see #getRepository() |
||||||
|
* |
||||||
|
*/ |
||||||
|
public void setRepository(File repository) { |
||||||
|
this.repository = repository; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the size threshold beyond which files are written directly to |
||||||
|
* disk. The default value is 10240 bytes. |
||||||
|
* |
||||||
|
* @return The size threshold, in bytes. |
||||||
|
* |
||||||
|
* @see #setSizeThreshold(int) |
||||||
|
*/ |
||||||
|
public int getSizeThreshold() { |
||||||
|
return sizeThreshold; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Sets the size threshold beyond which files are written directly to disk. |
||||||
|
* |
||||||
|
* @param sizeThreshold The size threshold, in bytes. |
||||||
|
* |
||||||
|
* @see #getSizeThreshold() |
||||||
|
* |
||||||
|
*/ |
||||||
|
public void setSizeThreshold(int sizeThreshold) { |
||||||
|
this.sizeThreshold = sizeThreshold; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public Methods
|
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new {@link com.fr.third.org.apache.commons.fileupload.disk.DiskFileItem} |
||||||
|
* instance from the supplied parameters and the local factory |
||||||
|
* configuration. |
||||||
|
* |
||||||
|
* @param fieldName The name of the form field. |
||||||
|
* @param contentType The content type of the form field. |
||||||
|
* @param isFormField <code>true</code> if this is a plain form field; |
||||||
|
* <code>false</code> otherwise. |
||||||
|
* @param fileName The name of the uploaded file, if any, as supplied |
||||||
|
* by the browser or other client. |
||||||
|
* |
||||||
|
* @return The newly created file item. |
||||||
|
*/ |
||||||
|
public FileItem createItem( |
||||||
|
String fieldName, |
||||||
|
String contentType, |
||||||
|
boolean isFormField, |
||||||
|
String fileName |
||||||
|
) { |
||||||
|
return new DiskFileItem(fieldName, contentType, |
||||||
|
isFormField, fileName, sizeThreshold, repository); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,58 @@ |
|||||||
|
<!-- |
||||||
|
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. |
||||||
|
--> |
||||||
|
<!-- $Id$ --> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>Overview of the com.fr.third.org.apache.commons.fileupload.disk component</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<p> |
||||||
|
A disk-based implementation of the |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileItem FileItem} |
||||||
|
interface. This implementation retains smaller items in memory, while |
||||||
|
writing larger ones to disk. The threshold between these two is |
||||||
|
configurable, as is the location of files that are written to disk. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
In typical usage, an instance of |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.disk.DiskFileItemFactory DiskFileItemFactory} |
||||||
|
would be created, configured, and then passed to a |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileUpload FileUpload} |
||||||
|
implementation such as |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.servlet.ServletFileUpload ServletFileUpload} |
||||||
|
or |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.portlet.PortletFileUpload PortletFileUpload}. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
The following code fragment demonstrates this usage. |
||||||
|
</p> |
||||||
|
<pre> |
||||||
|
DiskFileItemFactory factory = new DiskFileItemFactory(); |
||||||
|
// maximum size that will be stored in memory |
||||||
|
factory.setSizeThreshold(4096); |
||||||
|
// the location for saving data that is larger than getSizeThreshold() |
||||||
|
factory.setRepository(new File("/tmp")); |
||||||
|
|
||||||
|
ServletFileUpload upload = new ServletFileUpload(factory); |
||||||
|
</pre> |
||||||
|
<p> |
||||||
|
Please see the FileUpload |
||||||
|
<a href="http://jakarta.apache.org/commons/fileupload/using.html" target="_top">User Guide</a> |
||||||
|
for further details and examples of how to use this package. |
||||||
|
</p> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,90 @@ |
|||||||
|
<!-- |
||||||
|
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. |
||||||
|
--> |
||||||
|
<!-- $Id$ --> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>Overview of the com.fr.third.org.apache.commons.fileupload component</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<p> |
||||||
|
A component for handling HTML file uploads as specified by |
||||||
|
<a href="http://www.ietf.org/rfc/rfc1867.txt" target="_top">RFC 1867</a>. |
||||||
|
This component provides support for uploads within both servlets (JSR 53) |
||||||
|
and portlets (JSR 168). |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
While this package provides the generic functionality for file uploads, |
||||||
|
these classes are not typically used directly. Instead, normal usage |
||||||
|
involves one of the provided extensions of |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileUpload FileUpload} such as |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.servlet.ServletFileUpload ServletFileUpload} |
||||||
|
or |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.portlet.PortletFileUpload PortletFileUpload}, |
||||||
|
together with a factory for |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileItem FileItem} instances, |
||||||
|
such as |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.disk.DiskFileItemFactory DiskFileItemFactory}. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
The following is a brief example of typical usage in a servlet, storing |
||||||
|
the uploaded files on disk. |
||||||
|
</p> |
||||||
|
<pre> |
||||||
|
public void doPost(HttpServletRequest req, HttpServletResponse res) { |
||||||
|
DiskFileItemFactory factory = new DiskFileItemFactory(); |
||||||
|
// maximum size that will be stored in memory |
||||||
|
factory.setSizeThreshold(4096); |
||||||
|
// the location for saving data that is larger than getSizeThreshold() |
||||||
|
factory.setRepository(new File("/tmp")); |
||||||
|
|
||||||
|
ServletFileUpload upload = new ServletFileUpload(factory); |
||||||
|
// maximum size before a FileUploadException will be thrown |
||||||
|
upload.setSizeMax(1000000); |
||||||
|
|
||||||
|
List fileItems = upload.parseRequest(req); |
||||||
|
// assume we know there are two files. The first file is a small |
||||||
|
// text file, the second is unknown and is written to a file on |
||||||
|
// the server |
||||||
|
Iterator i = fileItems.iterator(); |
||||||
|
String comment = ((FileItem)i.next()).getString(); |
||||||
|
FileItem fi = (FileItem)i.next(); |
||||||
|
// filename on the client |
||||||
|
String fileName = fi.getName(); |
||||||
|
// save comment and filename to database |
||||||
|
... |
||||||
|
// write the file |
||||||
|
fi.write(new File("/www/uploads/", fileName)); |
||||||
|
} |
||||||
|
</pre> |
||||||
|
<p> |
||||||
|
In the example above, the first file is loaded into memory as a |
||||||
|
<code>String</code>. Before calling the <code>getString</code> method, |
||||||
|
the data may have been in memory or on disk depending on its size. The |
||||||
|
second file we assume it will be large and therefore never explicitly |
||||||
|
load it into memory, though if it is less than 4096 bytes it will be |
||||||
|
in memory before it is written to its final location. When writing to |
||||||
|
the final location, if the data is larger than the threshold, an attempt |
||||||
|
is made to rename the temporary file to the given location. If it cannot |
||||||
|
be renamed, it is streamed to the new location. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
Please see the FileUpload |
||||||
|
<a href="http://jakarta.apache.org/commons/fileupload/using.html" target="_top">User Guide</a> |
||||||
|
for further details and examples of how to use this package. |
||||||
|
</p> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,142 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.portlet; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import javax.portlet.ActionRequest; |
||||||
|
|
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileItemFactory; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileItemIterator; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileUpload; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileUploadBase; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileUploadException; |
||||||
|
|
||||||
|
/** |
||||||
|
* <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(javax.servlet.http.HttpServletRequest)} to acquire a list |
||||||
|
* of {@link com.fr.third.org.apache.commons.fileupload.FileItem FileItems} 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 |
||||||
|
* |
||||||
|
* @since FileUpload 1.1 |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public class PortletFileUpload extends FileUpload { |
||||||
|
|
||||||
|
// ---------------------------------------------------------- Class methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Utility method that determines whether the request contains multipart |
||||||
|
* content. |
||||||
|
* |
||||||
|
* @param request The portlet request 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(ActionRequest request) { |
||||||
|
return FileUploadBase.isMultipartContent( |
||||||
|
new PortletRequestContext(request)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an uninitialised instance of this class. A factory must be |
||||||
|
* configured, using <code>setFileItemFactory()</code>, before attempting |
||||||
|
* to parse requests. |
||||||
|
* |
||||||
|
* @see FileUpload#FileUpload(FileItemFactory) |
||||||
|
*/ |
||||||
|
public PortletFileUpload() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an instance of this class which uses the supplied factory to |
||||||
|
* create <code>FileItem</code> instances. |
||||||
|
* |
||||||
|
* @see FileUpload#FileUpload() |
||||||
|
* @param fileItemFactory The factory to use for creating file items. |
||||||
|
*/ |
||||||
|
public PortletFileUpload(FileItemFactory fileItemFactory) { |
||||||
|
super(fileItemFactory); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> |
||||||
|
* compliant <code>multipart/form-data</code> stream. |
||||||
|
* |
||||||
|
* @param request The portlet 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(ActionRequest request) |
||||||
|
throws FileUploadException { |
||||||
|
return parseRequest(new PortletRequestContext(request)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> |
||||||
|
* compliant <code>multipart/form-data</code> stream. |
||||||
|
* |
||||||
|
* @param request The portlet 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(ActionRequest request) |
||||||
|
throws FileUploadException, IOException { |
||||||
|
return super.getItemIterator(new PortletRequestContext(request)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,108 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.portlet; |
||||||
|
|
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import javax.portlet.ActionRequest; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.RequestContext; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p>Provides access to the request information needed for a request made to |
||||||
|
* a portlet.</p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* |
||||||
|
* @since FileUpload 1.1 |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public class PortletRequestContext implements RequestContext { |
||||||
|
|
||||||
|
// ----------------------------------------------------- Instance Variables
|
||||||
|
|
||||||
|
/** |
||||||
|
* The request for which the context is being provided. |
||||||
|
*/ |
||||||
|
private ActionRequest request; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
/** |
||||||
|
* Construct a context for this request. |
||||||
|
* |
||||||
|
* @param request The request to which this context applies. |
||||||
|
*/ |
||||||
|
public PortletRequestContext(ActionRequest request) { |
||||||
|
this.request = request; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public Methods
|
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the character encoding for the request. |
||||||
|
* |
||||||
|
* @return The character encoding for the request. |
||||||
|
*/ |
||||||
|
public String getCharacterEncoding() { |
||||||
|
return request.getCharacterEncoding(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the content type of the request. |
||||||
|
* |
||||||
|
* @return The content type of the request. |
||||||
|
*/ |
||||||
|
public String getContentType() { |
||||||
|
return request.getContentType(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the content length of the request. |
||||||
|
* |
||||||
|
* @return The content length of the request. |
||||||
|
*/ |
||||||
|
public int getContentLength() { |
||||||
|
return request.getContentLength(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the input stream for the request. |
||||||
|
* |
||||||
|
* @return The input stream for the request. |
||||||
|
* |
||||||
|
* @throws IOException if a problem occurs. |
||||||
|
*/ |
||||||
|
public InputStream getInputStream() throws IOException { |
||||||
|
return request.getPortletInputStream(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns a string representation of this object. |
||||||
|
* |
||||||
|
* @return a string representation of this object. |
||||||
|
*/ |
||||||
|
public String toString() { |
||||||
|
return "ContentLength=" |
||||||
|
+ this.getContentLength() |
||||||
|
+ ", ContentType=" |
||||||
|
+ this.getContentType(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
<!-- |
||||||
|
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. |
||||||
|
--> |
||||||
|
<!-- $Id$ --> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>Overview of the com.fr.third.org.apache.commons.fileupload.portlet component</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<p> |
||||||
|
An implementation of |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileUpload FileUpload} |
||||||
|
for use in portlets conforming to JSR 168. This implementation requires |
||||||
|
only access to the portlet's current <code>ActionRequest</code> instance, |
||||||
|
and a suitable |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileItemFactory FileItemFactory} |
||||||
|
implementation, such as |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.disk.DiskFileItemFactory DiskFileItemFactory}. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
The following code fragment demonstrates typical usage. |
||||||
|
</p> |
||||||
|
<pre> |
||||||
|
DiskFileItemFactory factory = new DiskFileItemFactory(); |
||||||
|
// Configure the factory here, if desired. |
||||||
|
PortletFileUpload upload = new PortletFileUpload(factory); |
||||||
|
// Configure the uploader here, if desired. |
||||||
|
List fileItems = upload.parseRequest(request); |
||||||
|
</pre> |
||||||
|
<p> |
||||||
|
Please see the FileUpload |
||||||
|
<a href="http://jakarta.apache.org/commons/fileupload/using.html" target="_top">User Guide</a> |
||||||
|
for further details and examples of how to use this package. |
||||||
|
</p> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.servlet; |
||||||
|
|
||||||
|
import javax.servlet.ServletContextListener; |
||||||
|
import javax.servlet.ServletContextEvent; |
||||||
|
|
||||||
|
import com.fr.third.org.apache.commons.io.FileCleaner; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* A servlet context listener, which ensures that the |
||||||
|
* {@link org.apache.commons.io.FileCleaner FileCleaner's} |
||||||
|
* reaper thread is terminated, |
||||||
|
* when the web application is destroyed. |
||||||
|
*/ |
||||||
|
public class FileCleanerCleanup implements ServletContextListener { |
||||||
|
/** |
||||||
|
* Called when the web application is initialized. Does |
||||||
|
* nothing. |
||||||
|
* @param sce The servlet context (ignored). |
||||||
|
*/ |
||||||
|
public void contextInitialized(ServletContextEvent sce) { |
||||||
|
// Does nothing.
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Called when the web application is being destroyed. |
||||||
|
* Calls {@link FileCleaner#exitWhenFinished()}. |
||||||
|
* @param sce The servlet context (ignored). |
||||||
|
*/ |
||||||
|
public void contextDestroyed(ServletContextEvent sce) { |
||||||
|
FileCleaner.exitWhenFinished(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,150 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.servlet; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
|
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileItemFactory; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileItemIterator; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileUpload; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.FileUploadException; |
||||||
|
|
||||||
|
/** |
||||||
|
* <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 |
||||||
|
* com.fr.third.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$ |
||||||
|
*/ |
||||||
|
public class ServletFileUpload extends FileUpload { |
||||||
|
|
||||||
|
// ---------------------------------------------------------- Class methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Utility method that determines whether the request contains multipart |
||||||
|
* content. |
||||||
|
* |
||||||
|
* @param request The servlet request 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( |
||||||
|
HttpServletRequest request) { |
||||||
|
if (!"post".equals(request.getMethod().toLowerCase())) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
String contentType = request.getContentType(); |
||||||
|
if (contentType == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
if (contentType.toLowerCase().startsWith(MULTIPART)) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an uninitialised instance of this class. A factory must be |
||||||
|
* configured, using <code>setFileItemFactory()</code>, before attempting |
||||||
|
* to parse requests. |
||||||
|
* |
||||||
|
* @see FileUpload#FileUpload(FileItemFactory) |
||||||
|
*/ |
||||||
|
public ServletFileUpload() { |
||||||
|
super(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Constructs an instance of this class which uses the supplied factory to |
||||||
|
* create <code>FileItem</code> instances. |
||||||
|
* |
||||||
|
* @see FileUpload#FileUpload() |
||||||
|
* @param fileItemFactory The factory to use for creating file items. |
||||||
|
*/ |
||||||
|
public ServletFileUpload(FileItemFactory fileItemFactory) { |
||||||
|
super(fileItemFactory); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public methods
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> |
||||||
|
* compliant <code>multipart/form-data</code> stream. |
||||||
|
* |
||||||
|
* @param request 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. |
||||||
|
*/ |
||||||
|
public List /* FileItem */ parseRequest(HttpServletRequest request) |
||||||
|
throws FileUploadException { |
||||||
|
return parseRequest(new ServletRequestContext(request)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a> |
||||||
|
* compliant <code>multipart/form-data</code> stream. |
||||||
|
* |
||||||
|
* @param request The servlet 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(HttpServletRequest request) |
||||||
|
throws FileUploadException, IOException { |
||||||
|
return super.getItemIterator(new ServletRequestContext(request)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,107 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.servlet; |
||||||
|
|
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import com.fr.third.org.apache.commons.fileupload.RequestContext; |
||||||
|
|
||||||
|
/** |
||||||
|
* <p>Provides access to the request information needed for a request made to |
||||||
|
* an HTTP servlet.</p> |
||||||
|
* |
||||||
|
* @author <a href="mailto:martinc@apache.org">Martin Cooper</a> |
||||||
|
* |
||||||
|
* @since FileUpload 1.1 |
||||||
|
* |
||||||
|
* @version $Id$ |
||||||
|
*/ |
||||||
|
public class ServletRequestContext implements RequestContext { |
||||||
|
|
||||||
|
// ----------------------------------------------------- Instance Variables
|
||||||
|
|
||||||
|
/** |
||||||
|
* The request for which the context is being provided. |
||||||
|
*/ |
||||||
|
private HttpServletRequest request; |
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------- Constructors
|
||||||
|
|
||||||
|
/** |
||||||
|
* Construct a context for this request. |
||||||
|
* |
||||||
|
* @param request The request to which this context applies. |
||||||
|
*/ |
||||||
|
public ServletRequestContext(HttpServletRequest request) { |
||||||
|
this.request = request; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public Methods
|
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the character encoding for the request. |
||||||
|
* |
||||||
|
* @return The character encoding for the request. |
||||||
|
*/ |
||||||
|
public String getCharacterEncoding() { |
||||||
|
return request.getCharacterEncoding(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the content type of the request. |
||||||
|
* |
||||||
|
* @return The content type of the request. |
||||||
|
*/ |
||||||
|
public String getContentType() { |
||||||
|
return request.getContentType(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the content length of the request. |
||||||
|
* |
||||||
|
* @return The content length of the request. |
||||||
|
*/ |
||||||
|
public int getContentLength() { |
||||||
|
return request.getContentLength(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve the input stream for the request. |
||||||
|
* |
||||||
|
* @return The input stream for the request. |
||||||
|
* |
||||||
|
* @throws IOException if a problem occurs. |
||||||
|
*/ |
||||||
|
public InputStream getInputStream() throws IOException { |
||||||
|
return request.getInputStream(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns a string representation of this object. |
||||||
|
* |
||||||
|
* @return a string representation of this object. |
||||||
|
*/ |
||||||
|
public String toString() { |
||||||
|
return "ContentLength=" |
||||||
|
+ this.getContentLength() |
||||||
|
+ ", ContentType=" |
||||||
|
+ this.getContentType(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
<!-- |
||||||
|
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. |
||||||
|
--> |
||||||
|
<!-- $Id$ --> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>Overview of the com.fr.third.org.apache.commons.fileupload.servlet component</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<p> |
||||||
|
An implementation of |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileUpload FileUpload} |
||||||
|
for use in servlets conforming to JSR 53. This implementation requires |
||||||
|
only access to the servlet's current <code>HttpServletRequest</code> |
||||||
|
instance, and a suitable |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.FileItemFactory FileItemFactory} |
||||||
|
implementation, such as |
||||||
|
{@link com.fr.third.org.apache.commons.fileupload.disk.DiskFileItemFactory DiskFileItemFactory}. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
The following code fragment demonstrates typical usage. |
||||||
|
</p> |
||||||
|
<pre> |
||||||
|
DiskFileItemFactory factory = new DiskFileItemFactory(); |
||||||
|
// Configure the factory here, if desired. |
||||||
|
ServletFileUpload upload = new ServletFileUpload(factory); |
||||||
|
// Configure the uploader here, if desired. |
||||||
|
List fileItems = upload.parseRequest(request); |
||||||
|
</pre> |
||||||
|
<p> |
||||||
|
Please see the FileUpload |
||||||
|
<a href="http://jakarta.apache.org/commons/fileupload/using.html" target="_top">User Guide</a> |
||||||
|
for further details and examples of how to use this package. |
||||||
|
</p> |
||||||
|
</body> |
||||||
|
</html> |
@ -0,0 +1,38 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.util; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* Interface of an object, which may be closed. |
||||||
|
*/ |
||||||
|
public interface Closeable { |
||||||
|
/** |
||||||
|
* Closes the object. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
*/ |
||||||
|
void close() throws IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns, whether the object is already closed. |
||||||
|
* @return True, if the object is closed, otherwise false. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
*/ |
||||||
|
boolean isClosed() throws IOException; |
||||||
|
} |
@ -0,0 +1,155 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.util; |
||||||
|
|
||||||
|
import java.io.FilterInputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* An input stream, which limits its data size. This stream is |
||||||
|
* used, if the content length is unknown. |
||||||
|
*/ |
||||||
|
public abstract class LimitedInputStream |
||||||
|
extends FilterInputStream implements Closeable { |
||||||
|
/** |
||||||
|
* The maximum size of an item, in bytes. |
||||||
|
*/ |
||||||
|
private long sizeMax; |
||||||
|
/** |
||||||
|
* The current number of bytes. |
||||||
|
*/ |
||||||
|
private long count; |
||||||
|
/** |
||||||
|
* Whether this stream is already closed. |
||||||
|
*/ |
||||||
|
private boolean closed; |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new instance. |
||||||
|
* @param pIn The input stream, which shall be limited. |
||||||
|
* @param pSizeMax The limit; no more than this number of bytes |
||||||
|
* shall be returned by the source stream. |
||||||
|
*/ |
||||||
|
public LimitedInputStream(InputStream pIn, long pSizeMax) { |
||||||
|
super(pIn); |
||||||
|
sizeMax = pSizeMax; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Called to indicate, that the input streams limit has |
||||||
|
* been exceeded. |
||||||
|
* @param pSizeMax The input streams limit, in bytes. |
||||||
|
* @param pCount The actual number of bytes. |
||||||
|
* @throws IOException The called method is expected |
||||||
|
* to raise an IOException. |
||||||
|
*/ |
||||||
|
protected abstract void raiseError(long pSizeMax, long pCount) |
||||||
|
throws IOException; |
||||||
|
|
||||||
|
/** Called to check, whether the input streams |
||||||
|
* limit is reached. |
||||||
|
* @throws IOException The given limit is exceeded. |
||||||
|
*/ |
||||||
|
private void checkLimit() throws IOException { |
||||||
|
if (count > sizeMax) { |
||||||
|
raiseError(sizeMax, count); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Reads the next byte of data from this input stream. The value |
||||||
|
* byte is returned as an <code>int</code> in the range |
||||||
|
* <code>0</code> to <code>255</code>. If no byte is available |
||||||
|
* because the end of the stream has been reached, the value |
||||||
|
* <code>-1</code> is returned. This method blocks until input data |
||||||
|
* is available, the end of the stream is detected, or an exception |
||||||
|
* is thrown. |
||||||
|
* <p> |
||||||
|
* This method |
||||||
|
* simply performs <code>in.read()</code> and returns the result. |
||||||
|
* |
||||||
|
* @return the next byte of data, or <code>-1</code> if the end of the |
||||||
|
* stream is reached. |
||||||
|
* @exception IOException if an I/O error occurs. |
||||||
|
* @see java.io.FilterInputStream#in |
||||||
|
*/ |
||||||
|
public int read() throws IOException { |
||||||
|
int res = super.read(); |
||||||
|
if (res != -1) { |
||||||
|
count++; |
||||||
|
checkLimit(); |
||||||
|
} |
||||||
|
return res; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Reads up to <code>len</code> bytes of data from this input stream |
||||||
|
* into an array of bytes. If <code>len</code> is not zero, the method |
||||||
|
* blocks until some input is available; otherwise, no |
||||||
|
* bytes are read and <code>0</code> is returned. |
||||||
|
* <p> |
||||||
|
* This method simply performs <code>in.read(b, off, len)</code> |
||||||
|
* and returns the result. |
||||||
|
* |
||||||
|
* @param b the buffer into which the data is read. |
||||||
|
* @param off The start offset in the destination array |
||||||
|
* <code>b</code>. |
||||||
|
* @param len the maximum number of bytes read. |
||||||
|
* @return the total number of bytes read into the buffer, or |
||||||
|
* <code>-1</code> if there is no more data because the end of |
||||||
|
* the stream has been reached. |
||||||
|
* @exception NullPointerException If <code>b</code> is <code>null</code>. |
||||||
|
* @exception IndexOutOfBoundsException If <code>off</code> is negative, |
||||||
|
* <code>len</code> is negative, or <code>len</code> is greater than |
||||||
|
* <code>b.length - off</code> |
||||||
|
* @exception IOException if an I/O error occurs. |
||||||
|
* @see java.io.FilterInputStream#in |
||||||
|
*/ |
||||||
|
public int read(byte[] b, int off, int len) throws IOException { |
||||||
|
int res = super.read(b, off, len); |
||||||
|
if (res > 0) { |
||||||
|
count += res; |
||||||
|
checkLimit(); |
||||||
|
} |
||||||
|
return res; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns, whether this stream is already closed. |
||||||
|
* @return True, if the stream is closed, otherwise false. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
*/ |
||||||
|
public boolean isClosed() throws IOException { |
||||||
|
return closed; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Closes this input stream and releases any system resources |
||||||
|
* associated with the stream. |
||||||
|
* This |
||||||
|
* method simply performs <code>in.close()</code>. |
||||||
|
* |
||||||
|
* @exception IOException if an I/O error occurs. |
||||||
|
* @see java.io.FilterInputStream#in |
||||||
|
*/ |
||||||
|
public void close() throws IOException { |
||||||
|
closed = true; |
||||||
|
super.close(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,166 @@ |
|||||||
|
/* |
||||||
|
* 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 com.fr.third.org.apache.commons.fileupload.util; |
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.OutputStream; |
||||||
|
|
||||||
|
|
||||||
|
/** Utility class for working with streams. |
||||||
|
*/ |
||||||
|
public final class Streams { |
||||||
|
/** |
||||||
|
* Private constructor, to prevent instantiation. |
||||||
|
* This class has only static methods. |
||||||
|
*/ |
||||||
|
private Streams() { |
||||||
|
// Does nothing
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Default buffer size for use in |
||||||
|
* {@link #copy(InputStream, OutputStream, boolean)}. |
||||||
|
*/ |
||||||
|
private static final int DEFAULT_BUFFER_SIZE = 8192; |
||||||
|
|
||||||
|
/** |
||||||
|
* Copies the contents of the given {@link InputStream} |
||||||
|
* to the given {@link OutputStream}. Shortcut for |
||||||
|
* <pre> |
||||||
|
* copy(pInputStream, pOutputStream, new byte[8192]); |
||||||
|
* </pre> |
||||||
|
* @param pInputStream The input stream, which is being read. |
||||||
|
* It is guaranteed, that {@link InputStream#close()} is called |
||||||
|
* on the stream. |
||||||
|
* @param pOutputStream The output stream, to which data should |
||||||
|
* be written. May be null, in which case the input streams |
||||||
|
* contents are simply discarded. |
||||||
|
* @param pClose True guarantees, that {@link OutputStream#close()} |
||||||
|
* is called on the stream. False indicates, that only |
||||||
|
* {@link OutputStream#flush()} should be called finally. |
||||||
|
* |
||||||
|
* @return Number of bytes, which have been copied. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
*/ |
||||||
|
public static long copy(InputStream pInputStream, |
||||||
|
OutputStream pOutputStream, boolean pClose) |
||||||
|
throws IOException { |
||||||
|
return copy(pInputStream, pOutputStream, pClose, |
||||||
|
new byte[DEFAULT_BUFFER_SIZE]); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Copies the contents of the given {@link InputStream} |
||||||
|
* to the given {@link OutputStream}. |
||||||
|
* @param pIn The input stream, which is being read. |
||||||
|
* It is guaranteed, that {@link InputStream#close()} is called |
||||||
|
* on the stream. |
||||||
|
* @param pOut The output stream, to which data should |
||||||
|
* be written. May be null, in which case the input streams |
||||||
|
* contents are simply discarded. |
||||||
|
* @param pClose True guarantees, that {@link OutputStream#close()} |
||||||
|
* is called on the stream. False indicates, that only |
||||||
|
* {@link OutputStream#flush()} should be called finally. |
||||||
|
* @param pBuffer Temporary buffer, which is to be used for |
||||||
|
* copying data. |
||||||
|
* @return Number of bytes, which have been copied. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
*/ |
||||||
|
public static long copy(InputStream pIn, |
||||||
|
OutputStream pOut, boolean pClose, |
||||||
|
byte[] pBuffer) |
||||||
|
throws IOException { |
||||||
|
OutputStream out = pOut; |
||||||
|
InputStream in = pIn; |
||||||
|
try { |
||||||
|
long total = 0; |
||||||
|
for (;;) { |
||||||
|
int res = in.read(pBuffer); |
||||||
|
if (res == -1) { |
||||||
|
break; |
||||||
|
} |
||||||
|
if (res > 0) { |
||||||
|
total += res; |
||||||
|
if (out != null) { |
||||||
|
out.write(pBuffer, 0, res); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
if (out != null) { |
||||||
|
if (pClose) { |
||||||
|
out.close(); |
||||||
|
} else { |
||||||
|
out.flush(); |
||||||
|
} |
||||||
|
out = null; |
||||||
|
} |
||||||
|
in.close(); |
||||||
|
in = null; |
||||||
|
return total; |
||||||
|
} finally { |
||||||
|
if (in != null) { |
||||||
|
try { |
||||||
|
in.close(); |
||||||
|
} catch (Throwable t) { |
||||||
|
/* Ignore me */ |
||||||
|
} |
||||||
|
} |
||||||
|
if (pClose && out != null) { |
||||||
|
try { |
||||||
|
out.close(); |
||||||
|
} catch (Throwable t) { |
||||||
|
/* Ignore me */ |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This convenience method allows to read a |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.FileItemStream}'s |
||||||
|
* content into a string. The platform's default character encoding |
||||||
|
* is used for converting bytes into characters. |
||||||
|
* @param pStream The input stream to read. |
||||||
|
* @see #asString(InputStream, String) |
||||||
|
* @return The streams contents, as a string. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
*/ |
||||||
|
public static String asString(InputStream pStream) throws IOException { |
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
||||||
|
copy(pStream, baos, true); |
||||||
|
return baos.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* This convenience method allows to read a |
||||||
|
* {@link com.fr.third.org.apache.commons.fileupload.FileItemStream}'s |
||||||
|
* content into a string, using the given character encoding. |
||||||
|
* @param pStream The input stream to read. |
||||||
|
* @param pEncoding The character encoding, typically "UTF-8". |
||||||
|
* @see #asString(InputStream) |
||||||
|
* @return The streams contents, as a string. |
||||||
|
* @throws IOException An I/O error occurred. |
||||||
|
*/ |
||||||
|
public static String asString(InputStream pStream, String pEncoding) |
||||||
|
throws IOException { |
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
||||||
|
copy(pStream, baos, true); |
||||||
|
return baos.toString(pEncoding); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,29 @@ |
|||||||
|
<!-- |
||||||
|
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. |
||||||
|
--> |
||||||
|
<!-- $Id: package.html 479262 2006-11-26 03:09:24Z niallp $ --> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
<title>Overview of the com.fr.third.org.apache.commons.fileupload.util component</title> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<p> |
||||||
|
This package contains various IO related utility classes |
||||||
|
or methods, which are basically reusable and not necessarily |
||||||
|
restricted to the scope of a file upload. |
||||||
|
</p> |
||||||
|
</body> |
||||||
|
</html> |
Loading…
Reference in new issue