KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openuss > utility > UploadFileHelper


1 /**
2  * Title: OpenUSS - Open Source University Support System
3  * Description: Utility Class for uploading the FileObject
4  * Copyright: Copyright (c) B. Lofi Dewanto
5  * Company: University of Muenster
6  * @author B. Lofi Dewanto
7  * @version 1.0
8  */

9 package org.openuss.utility;
10
11 import com.lutris.appserver.server.httpPresentation.*;
12
13 import com.lutris.mime.*;
14
15 //import com.lutris.xml.xmlc.*;
16

17 import java.io.*;
18
19 import java.rmi.*;
20
21 import java.text.*;
22
23 import java.util.*;
24
25 import org.w3c.dom.*;
26 import org.w3c.dom.html.*;
27
28
29 /**
30  * Helper for uploading a file.
31  * This class gets the data for uploading the file with its
32  * date, filename, title and the file object itself.
33  *
34  * @author B. Lofi Dewanto
35  * @version 1.0
36  */

37 public class UploadFileHelper {
38     // The contents of the update form
39
protected static String JavaDoc FILE_DATE = "Date";
40     protected static String JavaDoc FILE_NAME = "Filename";
41     protected static String JavaDoc FILE_TITLE = "Name";
42     protected static String JavaDoc FILE_FILE = "File";
43
44     // Data
45
protected String JavaDoc title;
46     protected String JavaDoc filename;
47     protected String JavaDoc date;
48     protected FileObjectWrapper fileObject;
49
50     /**
51      * Constructor.
52      */

53     public UploadFileHelper() throws Exception JavaDoc {
54         // Nothing to do
55
}
56
57     /**
58      * Constructor.
59      */

60     public UploadFileHelper(HttpPresentationComms comms)
61                      throws Exception JavaDoc {
62         this();
63
64
65         // Calculate...
66
calculateData(comms);
67     }
68
69     /**
70      * Get the title.
71      */

72     public String JavaDoc getTitle() {
73         return title;
74     }
75
76     /**
77      * Set the title.
78      */

79     public void setTitle(String JavaDoc title) {
80         this.title = title;
81     }
82
83     /**
84      * Get the date.
85      */

86     public String JavaDoc getDate() {
87         return date;
88     }
89
90     /**
91      * Set the date.
92      */

93     public void setDate(String JavaDoc date) {
94         this.date = date;
95     }
96
97     /**
98      * Get the filename.
99      */

100     public String JavaDoc getFilename() {
101         return filename;
102     }
103
104     /**
105      * Set the filename.
106      */

107     public void setFilename(String JavaDoc filename) {
108         this.filename = filename;
109     }
110
111     /**
112      * Get the size.
113      */

114     public long getSize() {
115         if (fileObject != null) {
116             // Calculate the size of the FileObject
117
byte[] data = fileObject.getData();
118             long fileSize = data.length;
119
120             // Put this in kilobyte, don't forget to
121
// add one on it
122
long result = (fileSize / 1024) + 1;
123
124             return result;
125         } else {
126             // No file
127
return 0;
128         }
129     }
130
131     /**
132      * Get the file.
133      */

134     public FileObjectWrapper getFileObject() {
135         return fileObject;
136     }
137
138     /**
139      * Set the file.
140      */

141     public void setFileObject(FileObjectWrapper fileObject) {
142         this.fileObject = fileObject;
143     }
144
145     /**
146      * Calculate the posted data.
147      */

148     protected void calculateData(HttpPresentationComms comms)
149                           throws Exception JavaDoc {
150         // Get the content type first
151
String JavaDoc contentType = comms.request.getContentType();
152
153         if (contentType == null) {
154             contentType = "";
155         }
156
157         // Create the content header
158
ContentHeader contentHdr = new ContentHeader("Content-Type: " +
159                                                      contentType);
160
161         // Get the input stream
162
HttpPresentationInputStream input = comms.request.getInputStream();
163
164         // Create the multiple mime
165
MultipartMimeInput mime = new MultipartMimeInput(input, contentHdr);
166         MultipartMimeInputStream in;
167
168         // Go through the multiple content...
169
while ((in = mime.nextPart()) != null) {
170             MimeHeader hdr = in.getHeader("Content-Disposition");
171
172             // Check the header
173
if (hdr != null) {
174                 String JavaDoc contentName = null;
175
176                 ContentHeader chdr = new ContentHeader(hdr);
177                 contentName = chdr.getParameter("name");
178
179                 // --- Get the file ---
180
if (contentName.equals(FILE_FILE)) {
181                     /* !!!For testing purpose: Directly writing the file to the
182                        file system!!!
183                     int n;
184                     byte[] buffer = new byte[1024];
185                     String outFileName = "c:/temp/" + contentName +
186                       System.currentTimeMillis();
187                                                                                            
188                     FileOutputStream out = new FileOutputStream(outFileName);
189                                                                                            
190                     while ((n = in.read(buffer, 0, buffer.length)) > 0) {
191                       out.write(buffer, 0, n);
192                     }
193                                                                                            
194                     out.close();
195                      */

196
197                     // Read the stream
198
int n;
199                     byte[] buffer = new byte[1024];
200
201                     ByteArrayOutputStream outByte = new ByteArrayOutputStream();
202
203                     while ((n = in.read(buffer, 0, buffer.length)) > 0) {
204                         outByte.write(buffer, 0, n);
205                     }
206
207
208                     // Save into the FileObject
209
outByte.flush();
210
211                     byte[] data = outByte.toByteArray();
212
213                     // Save into the FileObject, if the size bigger than zero
214
if (outByte.size() > 0) {
215                         fileObject = new FileObjectWrapper(data);
216                     }
217
218                     outByte.close();
219                 }
220
221                 // --- Get the title ---
222
if (contentName.equals(FILE_TITLE)) {
223                     int n;
224                     byte[] buffer = new byte[1024];
225
226                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
227
228                     while ((n = in.read(buffer, 0, buffer.length)) > 0) {
229                         for (int i = 0; i < n; i++) {
230                             sb.append((char) (((int) (buffer[i]) + 0x100) &
231                                           0xff));
232                         }
233                     }
234
235
236                     // Save in the private variable
237
title = new String JavaDoc(sb);
238                 }
239
240                 // --- Get the date ---
241
if (contentName.equals(FILE_DATE)) {
242                     int n;
243                     byte[] buffer = new byte[1024];
244
245                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
246
247                     while ((n = in.read(buffer, 0, buffer.length)) > 0) {
248                         for (int i = 0; i < n; i++) {
249                             sb.append((char) (((int) (buffer[i]) + 0x100) &
250                                           0xff));
251                         }
252                     }
253
254
255                     // Save in the private variable
256
date = new String JavaDoc(sb);
257                 }
258
259                 // --- Get the filename ---
260
if (contentName.equals(FILE_NAME)) {
261                     int n;
262                     byte[] buffer = new byte[1024];
263
264                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
265
266                     while ((n = in.read(buffer, 0, buffer.length)) > 0) {
267                         for (int i = 0; i < n; i++) {
268                             sb.append((char) (((int) (buffer[i]) + 0x100) &
269                                           0xff));
270                         }
271                     }
272
273
274                     // Save in the private variable
275
filename = new String JavaDoc(sb);
276                 }
277             }
278
279
280             // Close the input but not the whole upload
281
in.close();
282         }
283     }
284 }
Popular Tags