1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.fileupload.disk;
17
18 import java.io.File;
19
20 import org.apache.commons.fileupload.FileItem;
21 import org.apache.commons.fileupload.FileItemFactory;
22
23 /***
24 * <p>The default {@link org.apache.commons.fileupload.FileItemFactory}
25 * implementation. This implementation creates
26 * {@link org.apache.commons.fileupload.FileItem} instances which keep their
27 * content either in memory, for smaller items, or in a temporary file on disk,
28 * for larger items. The size threshold, above which content will be stored on
29 * disk, is configurable, as is the directory in which temporary files will be
30 * created.</p>
31 *
32 * <p>If not otherwise configured, the default configuration values are as
33 * follows:
34 * <ul>
35 * <li>Size threshold is 10KB.</li>
36 * <li>Repository is the system default temp directory, as returned by
37 * <code>System.getProperty("java.io.tmpdir")</code>.</li>
38 * </ul>
39 * </p>
40 *
41 * @author <a href="mailto:martinc@apache.org">Martin Cooper</a>
42 *
43 * @since FileUpload 1.1
44 *
45 * @version $Id: DiskFileItemFactory.java 155417 2005-02-26 13:00:27Z dirkv $
46 */
47 public class DiskFileItemFactory implements FileItemFactory {
48
49
50
51
52 /***
53 * The default threshold above which uploads will be stored on disk.
54 */
55 public static final int DEFAULT_SIZE_THRESHOLD = 10240;
56
57
58
59
60
61 /***
62 * The directory in which uploaded files will be stored, if stored on disk.
63 */
64 private File repository;
65
66
67 /***
68 * The threshold above which uploads will be stored on disk.
69 */
70 private int sizeThreshold = DEFAULT_SIZE_THRESHOLD;
71
72
73
74
75
76 /***
77 * Constructs an unconfigured instance of this class. The resulting factory
78 * may be configured by calling the appropriate setter methods.
79 */
80 public DiskFileItemFactory() {
81 }
82
83
84 /***
85 * Constructs a preconfigured instance of this class.
86 *
87 * @param sizeThreshold The threshold, in bytes, below which items will be
88 * retained in memory and above which they will be
89 * stored as a file.
90 * @param repository The data repository, which is the directory in
91 * which files will be created, should the item size
92 * exceed the threshold.
93 */
94 public DiskFileItemFactory(int sizeThreshold, File repository) {
95 this.sizeThreshold = sizeThreshold;
96 this.repository = repository;
97 }
98
99
100
101
102
103 /***
104 * Returns the directory used to temporarily store files that are larger
105 * than the configured size threshold.
106 *
107 * @return The directory in which temporary files will be located.
108 *
109 * @see #setRepository(java.io.File)
110 *
111 */
112 public File getRepository() {
113 return repository;
114 }
115
116
117 /***
118 * Sets the directory used to temporarily store files that are larger
119 * than the configured size threshold.
120 *
121 * @param repository The directory in which temporary files will be located.
122 *
123 * @see #getRepository()
124 *
125 */
126 public void setRepository(File repository) {
127 this.repository = repository;
128 }
129
130
131 /***
132 * Returns the size threshold beyond which files are written directly to
133 * disk. The default value is 1024 bytes.
134 *
135 * @return The size threshold, in bytes.
136 *
137 * @see #setSizeThreshold(int)
138 */
139 public int getSizeThreshold() {
140 return sizeThreshold;
141 }
142
143
144 /***
145 * Sets the size threshold beyond which files are written directly to disk.
146 *
147 * @param sizeThreshold The size threshold, in bytes.
148 *
149 * @see #getSizeThreshold()
150 *
151 */
152 public void setSizeThreshold(int sizeThreshold) {
153 this.sizeThreshold = sizeThreshold;
154 }
155
156
157
158
159 /***
160 * Create a new {@link org.apache.commons.fileupload.disk.DiskFileItem}
161 * instance from the supplied parameters and the local factory
162 * configuration.
163 *
164 * @param fieldName The name of the form field.
165 * @param contentType The content type of the form field.
166 * @param isFormField <code>true</code> if this is a plain form field;
167 * <code>false</code> otherwise.
168 * @param fileName The name of the uploaded file, if any, as supplied
169 * by the browser or other client.
170 *
171 * @return The newly created file item.
172 */
173 public FileItem createItem(
174 String fieldName,
175 String contentType,
176 boolean isFormField,
177 String fileName
178 ) {
179 return new DiskFileItem(fieldName, contentType,
180 isFormField, fileName, sizeThreshold, repository);
181 }
182
183 }