1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.fileupload.servlet;
18
19 import java.io.IOException;
20 import java.util.List;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import org.apache.commons.fileupload.FileItemFactory;
25 import org.apache.commons.fileupload.FileItemIterator;
26 import org.apache.commons.fileupload.FileUpload;
27 import org.apache.commons.fileupload.FileUploadException;
28
29 /**
30 * <p>High level API for processing file uploads.</p>
31 *
32 * <p>This class handles multiple files per single HTML widget, sent using
33 * <code>multipart/mixed</code> encoding type, as specified by
34 * <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>. Use {@link
35 * #parseRequest(HttpServletRequest)} to acquire a list of {@link
36 * org.apache.commons.fileupload.FileItem}s associated with a given HTML
37 * widget.</p>
38 *
39 * <p>How the data for individual parts is stored is determined by the factory
40 * used to create them; a given part may be in memory, on disk, or somewhere
41 * else.</p>
42 *
43 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
44 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
45 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
46 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
47 * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
48 * @author Sean C. Sullivan
49 *
50 * @version $Id: ServletFileUpload.java 479484 2006-11-27 01:06:53Z jochen $
51 */
52 public class ServletFileUpload extends FileUpload {
53
54 // ---------------------------------------------------------- Class methods
55
56
57 /**
58 * Utility method that determines whether the request contains multipart
59 * content.
60 *
61 * @param request The servlet request to be evaluated. Must be non-null.
62 *
63 * @return <code>true</code> if the request is multipart;
64 * <code>false</code> otherwise.
65 */
66 public static final boolean isMultipartContent(
67 HttpServletRequest request) {
68 if (!"post".equals(request.getMethod().toLowerCase())) {
69 return false;
70 }
71 String contentType = request.getContentType();
72 if (contentType == null) {
73 return false;
74 }
75 if (contentType.toLowerCase().startsWith(MULTIPART)) {
76 return true;
77 }
78 return false;
79 }
80
81
82 // ----------------------------------------------------------- Constructors
83
84
85 /**
86 * Constructs an uninitialised instance of this class. A factory must be
87 * configured, using <code>setFileItemFactory()</code>, before attempting
88 * to parse requests.
89 *
90 * @see FileUpload#FileUpload(FileItemFactory)
91 */
92 public ServletFileUpload() {
93 super();
94 }
95
96
97 /**
98 * Constructs an instance of this class which uses the supplied factory to
99 * create <code>FileItem</code> instances.
100 *
101 * @see FileUpload#FileUpload()
102 * @param fileItemFactory The factory to use for creating file items.
103 */
104 public ServletFileUpload(FileItemFactory fileItemFactory) {
105 super(fileItemFactory);
106 }
107
108
109 // --------------------------------------------------------- Public methods
110
111
112 /**
113 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
114 * compliant <code>multipart/form-data</code> stream.
115 *
116 * @param request The servlet request to be parsed.
117 *
118 * @return A list of <code>FileItem</code> instances parsed from the
119 * request, in the order that they were transmitted.
120 *
121 * @throws FileUploadException if there are problems reading/parsing
122 * the request or storing files.
123 */
124 public List /* FileItem */ parseRequest(HttpServletRequest request)
125 throws FileUploadException {
126 return parseRequest(new ServletRequestContext(request));
127 }
128
129
130 /**
131 * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
132 * compliant <code>multipart/form-data</code> stream.
133 *
134 * @param request The servlet request to be parsed.
135 *
136 * @return An iterator to instances of <code>FileItemStream</code>
137 * parsed from the request, in the order that they were
138 * transmitted.
139 *
140 * @throws FileUploadException if there are problems reading/parsing
141 * the request or storing files.
142 * @throws IOException An I/O error occurred. This may be a network
143 * error while communicating with the client or a problem while
144 * storing the uploaded content.
145 */
146 public FileItemIterator getItemIterator(HttpServletRequest request)
147 throws FileUploadException, IOException {
148 return super.getItemIterator(new ServletRequestContext(request));
149 }
150 }