KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > UploadedFile


1 package jodd.servlet;
2
3 import java.io.IOException;
4
5 import jodd.file.FileUtil;
6
7 /**
8  * Represents uploaded file. Created by MultipartRequest. An instance may be
9  * valid, when it represent a file, or invalid when uploaded file doesn't
10  * exist.
11  *
12  * @see jodd.servlet.MultipartRequest
13  */

14 public class UploadedFile {
15
16     private boolean isvalid = true;
17
18     /**
19      * Returns flag that indicates if file was loaded (valid).
20      *
21      * @return <code>true</code> for valid files, <code>false</code> for files that
22      * didn't exist at the upload time.
23      */

24     public boolean isValid() {
25         return isvalid;
26     }
27
28     /**
29      * Contrcuts new valid uploaded file.
30      */

31     public UploadedFile() {
32         isvalid = true;
33     }
34
35     /**
36      * Contrcuts new uploaded file.
37      *
38      * @param valid <code>true</code> for valid files, <code>false</code> for files that
39      * didn't exist at the upload time.s
40      */

41     public UploadedFile(boolean valid) {
42         isvalid = valid;
43     }
44
45     // ---------------------------------------------------------------- names
46

47     private String fieldName;
48     /**
49      * Sets form field name.
50      *
51      * @param v form field name
52      */

53     public void setFieldName(String v) {
54         fieldName = v;
55     }
56     /**
57      * Returns form field name.
58      *
59      * @return form field name
60      * @see #getFileExt
61      */

62     public String getFieldName() {
63         return fieldName;
64     }
65
66
67     private String fileName;
68     /**
69      * Sets file name and file extension. File name doesn't contain any path data
70      * and do contains file extension.
71      *
72      * @param v file name
73      *
74      * @see #getFileExt
75      */

76     private void setFileName(String v) {
77         fileName = v;
78         if (fileName == null) {
79             fileExt = null;
80             return;
81         }
82         int start = fileName.lastIndexOf('.') + 1;
83         if (start == -1) {
84             fileExt = "";
85             return;
86         }
87         fileExt = fileName.substring(start);
88     }
89     /**
90      * Returns file name. File name doesn't contain any path data.
91      *
92      * @return file name
93      */

94     public String getFileName() {
95         return fileName;
96     }
97
98     private String fileExt;
99     /**
100      * Returns file extension.
101      *
102      * @return file extension
103      */

104     public String getFileExt() {
105         return fileExt;
106     }
107
108
109     private String filePathName;
110     /**
111      * Sets the complete path name.
112      *
113      * @param v full file path name
114      */

115     public void setFilePathName(String v) {
116         filePathName = v;
117         if (filePathName == null) {
118             setFileName(null);
119             return;
120         }
121         int pos = filePathName.lastIndexOf('/');
122         if (pos != -1) {
123             setFileName(filePathName.substring(pos + 1));
124             return;
125         }
126         pos = filePathName.lastIndexOf('\\');
127         if (pos != -1) {
128             setFileName(filePathName.substring(pos + 1));
129             return;
130         }
131         setFileName(filePathName);
132     }
133
134     /**
135      * Return complete file name with path included.
136      *
137      * @return file path and name
138      */

139     public String getFilePathName() {
140         return filePathName;
141     }
142
143
144     // ---------------------------------------------------------------- content
145

146     private String contentType;
147     /**
148      * Sets file content type and reads MIME and subtype MIME.
149      *
150      * @param v content type
151      */

152     public void setContentType(String v) {
153         contentType = v;
154         typeMIME = getTypeMIME(contentType);
155         subTypeMIME = getSubTypeMIME(contentType);
156     }
157     /**
158      * Returns files content type.
159      *
160      * @return content type
161      */

162     public String getContentType() {
163         return contentType;
164     }
165     private String typeMIME;
166     /**
167      * Returns file types MIME.
168      *
169      * @return file MIME
170      */

171     public String getTypeMIME() {
172         return typeMIME;
173     }
174     private String subTypeMIME;
175     /**
176      * Returns file sub type MIME.
177      *
178      * @return sub type MIME
179      */

180     public String getSubTypeMIME() {
181         return subTypeMIME;
182     }
183     
184
185     private String contentDisp;
186     /**
187      * Sets content disp.
188      *
189      * @param v content disp
190      */

191     public void setContentDisp(String v) {
192         contentDisp = v;
193     }
194     /**
195      * Gets content disp.
196      *
197      * @return content disp
198      */

199     public String getContentDisp() {
200         return contentDisp;
201     }
202     
203     // ---------------------------------------------------------------- size and position
204

205     private int size;
206     /**
207      * Sets the file size
208      *
209      * @param v file size
210      */

211     public void setSize(int v) {
212         if (v < 0) {
213             v = 0;
214         }
215         size = v;
216     }
217     /**
218      * Returns the file size
219      *
220      * @return file size
221      */

222     public int getSize() {
223         return size;
224     }
225
226     private int startData;
227     /**
228      * Sets starting index of a file in requet data.
229      *
230      * @param v starting index
231      */

232     public void setDataStart(int v) {
233         if (v < 0) {
234             v = 0;
235         }
236         startData = v;
237     }
238     /**
239      * Returns starting index of a file in request data.
240      *
241      * @return starting index
242      */

243     public int getDataStart() {
244         return startData;
245     }
246
247     /**
248      * Returns ending index of a file in request data
249      *
250      * @return ending index
251      */

252     public int getDataEnd() {
253         return startData + size - 1;
254     }
255     
256     
257     // ---------------------------------------------------------------- data content
258

259     private byte[] requestData;
260
261     /**
262      * Sets request data.
263      *
264      * @param v request data
265      *
266      * @see #getRequestData
267      */

268     public void setRequestData(byte[] v) {
269         requestData = v;
270     }
271     /**
272      * Returns complete request. Should not be used much.
273      *
274      * @return array of request data
275      */

276     public byte[] getRequestData() {
277         return requestData;
278     }
279
280     /**
281      * Returns file data as a byte array.
282      *
283      * @return raw file data
284      */

285     public byte[] getData() {
286         if (size < 0) {
287             return null;
288         }
289         if (requestData == null) {
290             return null;
291         }
292         byte[] result = new byte[size];
293         System.arraycopy(requestData, startData, result, 0, size);
294         return result;
295     }
296
297     /**
298      * Writes data to a file.
299      *
300      * @param fileName name of the file where to store data
301      *
302      * @exception IOException
303      */

304     public void writeData(String fileName) throws IOException {
305         FileUtil.writeBytes(fileName, requestData, startData, size);
306         
307     }
308
309
310     // ---------------------------------------------------------------- misc
311

312
313     private String getTypeMIME(String ContentType) {
314         int pos = ContentType.indexOf("/");
315         if (pos == -1) {
316             return ContentType;
317         }
318         return ContentType.substring(1, pos);
319     }
320
321     private String getSubTypeMIME(String ContentType) {
322         String value = new String();
323         int start = ContentType.indexOf("/");
324         if (start == -1) {
325             return ContentType;
326         }
327         start++;
328         return ContentType.substring(start);
329     }
330
331     // ---------------------------------------------------------------- toString
332

333
334     public String toString() {
335         return "UploadedFile: valid=" + isvalid + " fieldName=" + fieldName + " fileName=" + fileName;
336     }
337
338 }
339
Popular Tags