1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.fileupload;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.UnsupportedEncodingException;
26
27 import org.apache.commons.fileupload.FileItem;
28 import org.apache.commons.fileupload.FileItemFactory;
29 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
30
31
32
33
34
35
36
37
38
39
40
41 public class StreamFileItemFactory implements FileItemFactory {
42
43
44 public static final int DEFAULT_INITIAL_BUFFER_SIZE = 8192;
45
46
47 private int initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE;
48
49
50
51
52
53
54
55 public void setInitialBufferSize(final int initialBufferSize) {
56 this.initialBufferSize = initialBufferSize;
57 }
58
59
60
61
62 public FileItem createItem(final String fieldName,
63 final String contentType, final boolean isFormField,
64 final String fileName) {
65 return new ByteArrayFileItem(fieldName, contentType, isFormField,
66 fileName, initialBufferSize);
67 }
68
69
70
71
72
73
74 private static class ByteArrayFileItem implements FileItem {
75
76 private static final long serialVersionUID = 1L;
77
78
79 private String fieldName;
80
81
82 private final String contentType;
83
84
85 private boolean isFormField;
86
87
88 private final String fileName;
89
90
91 private final int initialBufferSize;
92
93
94 private transient ByteArrayOutputStream outputStream;
95
96
97 private byte[] data;
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public ByteArrayFileItem(final String fieldName,
114 final String contentType, final boolean isFormField,
115 final String fileName, final int initialBufferSize) {
116 this.fieldName = fieldName;
117 this.contentType = contentType;
118 this.isFormField = isFormField;
119 this.fileName = fileName;
120 this.initialBufferSize = initialBufferSize;
121 }
122
123
124
125
126 public InputStream getInputStream() throws IOException {
127 return new ByteArrayInputStream(get());
128 }
129
130
131
132
133 public String getContentType() {
134 return contentType;
135 }
136
137
138
139
140 public String getName() {
141 return fileName;
142 }
143
144
145
146
147 public boolean isInMemory() {
148 return true;
149 }
150
151
152
153
154 public long getSize() {
155 return get().length;
156 }
157
158
159
160
161 public byte[] get() {
162 if (data == null) {
163 data = outputStream.toByteArray();
164 }
165 return data;
166 }
167
168
169
170
171 public String getString(final String encoding)
172 throws UnsupportedEncodingException {
173 return new String(get(), encoding);
174 }
175
176
177
178
179 public String getString() {
180 return new String(get());
181 }
182
183
184
185
186 public void write(final File file) throws Exception {
187 final FileOutputStream out = new FileOutputStream(file);
188 try {
189 out.write(get());
190 } finally {
191 if (out != null) {
192 out.close();
193 }
194 }
195 }
196
197
198
199
200 public void delete() {
201 this.data = null;
202 }
203
204
205
206
207 public String getFieldName() {
208 return fieldName;
209 }
210
211
212
213
214 public void setFieldName(final String name) {
215 this.fieldName = name;
216 }
217
218
219
220
221 public boolean isFormField() {
222 return isFormField;
223 }
224
225
226
227
228 public void setFormField(final boolean state) {
229 this.isFormField = state;
230 }
231
232
233
234
235 public OutputStream getOutputStream() {
236 if (outputStream == null) {
237 outputStream = new ByteArrayOutputStream(initialBufferSize);
238 }
239 return outputStream;
240 }
241
242 }
243 }