KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > upload > FileUploadHeader


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.servlet.upload;
4
5 import jodd.io.FileNameUtil;
6
7 /**
8  * Parses file upload header.
9  */

10 public class FileUploadHeader {
11
12     String JavaDoc dataHeader;
13     String JavaDoc formFieldName;
14
15     String JavaDoc formFileName;
16     String JavaDoc path;
17     String JavaDoc fileName;
18
19     boolean isFile;
20     String JavaDoc contentType;
21     String JavaDoc mimeType;
22     String JavaDoc mimeSubtype;
23     String JavaDoc contentDisposition;
24
25
26     FileUploadHeader(String JavaDoc dataHeader) {
27         this.dataHeader = dataHeader;
28         isFile = dataHeader.indexOf("filename") > 0;
29         formFieldName = getDataFieldValue(dataHeader, "name");
30         if (isFile) {
31             formFileName = getDataFieldValue(dataHeader, "filename");
32             if (formFileName == null) {
33                 return;
34             }
35             if (formFileName.length() == 0) {
36                 path = "";
37                 fileName = "";
38             }
39             int ls = FileNameUtil.indexOfLastSeparator(formFileName);
40             if (ls == -1) {
41                 path = "";
42                 fileName = formFileName;
43             } else {
44                 path = formFileName.substring(0, ls);
45                 fileName = formFileName.substring(ls);
46             }
47             if (fileName.length() > 0) {
48                 this.contentType = getContentType(dataHeader);
49                 mimeType = getMimeType(contentType);
50                 mimeSubtype = getMimeSubtype(contentType);
51                 contentDisposition = getContentDisposition(dataHeader);
52             }
53         }
54     }
55
56     // ---------------------------------------------------------------- utilities
57

58     /**
59      * Gets value of data field or <code>null</code> if field not found.
60      */

61     private String JavaDoc getDataFieldValue(String JavaDoc dataHeader, String JavaDoc fieldName) {
62         String JavaDoc value = null;
63         String JavaDoc token = String.valueOf((new StringBuffer JavaDoc(String.valueOf(fieldName))).append('=').append('"'));
64         int pos = dataHeader.indexOf(token);
65         if (pos > 0) {
66             int start = pos + token.length();
67             int end = dataHeader.indexOf('"', start);
68             if ((start > 0) && (end > 0)) {
69                 value = dataHeader.substring(start, end);
70             }
71         }
72         return value;
73     }
74
75     /**
76      * Strips content type information from request's data header.
77      * @param dataHeader data header string
78      * @return content type or an empty string if no content type defined
79      */

80     private String JavaDoc getContentType(String JavaDoc dataHeader) {
81         String JavaDoc token = "Content-Type:";
82         int start = dataHeader.indexOf(token);
83         if (start == -1) {
84             return "";
85         }
86         start += token.length();
87         return dataHeader.substring(start);
88     }
89
90     private String JavaDoc getContentDisposition(String JavaDoc dataHeader) {
91         int start = dataHeader.indexOf(':') + 1;
92         int end = dataHeader.indexOf(';');
93         return dataHeader.substring(start, end);
94     }
95
96     private String JavaDoc getMimeType(String JavaDoc ContentType) {
97         int pos = ContentType.indexOf('/');
98         if (pos == -1) {
99             return ContentType;
100         }
101         return ContentType.substring(1, pos);
102     }
103
104     private String JavaDoc getMimeSubtype(String JavaDoc ContentType) {
105         int start = ContentType.indexOf('/');
106         if (start == -1) {
107             return ContentType;
108         }
109         start++;
110         return ContentType.substring(start);
111     }
112
113
114     // ---------------------------------------------------------------- public interface
115

116     /**
117      * Returns <code>true</code> if uploaded data are correctly marked as a file.
118      * This is true if header contains string 'filename'.
119      */

120     public boolean isFile() {
121         return isFile;
122     }
123
124     /**
125      * Returns form field name.
126      */

127     public String JavaDoc getFormFieldName() {
128         return formFieldName;
129     }
130
131     /**
132      * Returns complete file name as specified at client side.
133      */

134     public String JavaDoc getFormFilename() {
135         return formFileName;
136     }
137
138     /**
139      * Returns file name (base name and extension, without full path data).
140      */

141     public String JavaDoc getFileName() {
142         return fileName;
143     }
144
145     /**
146      * Returns uploaded content type. It is usually in the following form:<br>
147      * mime_type/mime_subtype.
148      *
149      * @see #getMimeType()
150      * @see #getMimeSubtype()
151      */

152     public String JavaDoc getContentType() {
153         return contentType;
154     }
155
156     /**
157      * Returns file types MIME.
158      */

159     public String JavaDoc getMimeType() {
160         return mimeType;
161     }
162
163     /**
164      * Returns file sub type MIME.
165      */

166     public String JavaDoc getMimeSubtype() {
167         return mimeSubtype;
168     }
169
170     /**
171      * Returns content disposition. Usually it is 'form-data'.
172      */

173     public String JavaDoc getContentDisposition() {
174         return contentDisposition;
175     }
176 }
177
Popular Tags