Source Code Cross Referenced for UploadFileHelper.java in  » J2EE » Enhydra-Demos » org » openuss » utility » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » Enhydra Demos » org.openuss.utility 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Title:        OpenUSS - Open Source University Support System
003:         * Description:  Utility Class for uploading the FileObject
004:         * Copyright:    Copyright (c) B. Lofi Dewanto
005:         * Company:      University of Muenster
006:         * @author  B. Lofi Dewanto
007:         * @version 1.0
008:         */package org.openuss.utility;
009:
010:        import com.lutris.appserver.server.httpPresentation.*;
011:
012:        import com.lutris.mime.*;
013:
014:        //import com.lutris.xml.xmlc.*;
015:
016:        import java.io.*;
017:
018:        import java.rmi.*;
019:
020:        import java.text.*;
021:
022:        import java.util.*;
023:
024:        import org.w3c.dom.*;
025:        import org.w3c.dom.html.*;
026:
027:        /**
028:         * Helper for uploading a file.
029:         * This class gets the data for uploading the file with its
030:         * date, filename, title and the file object itself.
031:         *
032:         * @author  B. Lofi Dewanto
033:         * @version 1.0
034:         */
035:        public class UploadFileHelper {
036:            // The contents of the update form
037:            protected static String FILE_DATE = "Date";
038:            protected static String FILE_NAME = "Filename";
039:            protected static String FILE_TITLE = "Name";
040:            protected static String FILE_FILE = "File";
041:
042:            // Data
043:            protected String title;
044:            protected String filename;
045:            protected String date;
046:            protected FileObjectWrapper fileObject;
047:
048:            /**
049:             * Constructor.
050:             */
051:            public UploadFileHelper() throws Exception {
052:                // Nothing to do  
053:            }
054:
055:            /**
056:             * Constructor.
057:             */
058:            public UploadFileHelper(HttpPresentationComms comms)
059:                    throws Exception {
060:                this ();
061:
062:                // Calculate...
063:                calculateData(comms);
064:            }
065:
066:            /**
067:             * Get the title.
068:             */
069:            public String getTitle() {
070:                return title;
071:            }
072:
073:            /**
074:             * Set the title.
075:             */
076:            public void setTitle(String title) {
077:                this .title = title;
078:            }
079:
080:            /**
081:             * Get the date.
082:             */
083:            public String getDate() {
084:                return date;
085:            }
086:
087:            /**
088:             * Set the date.
089:             */
090:            public void setDate(String date) {
091:                this .date = date;
092:            }
093:
094:            /**
095:             * Get the filename.
096:             */
097:            public String getFilename() {
098:                return filename;
099:            }
100:
101:            /**
102:             * Set the filename.
103:             */
104:            public void setFilename(String filename) {
105:                this .filename = filename;
106:            }
107:
108:            /**
109:             * Get the size.
110:             */
111:            public long getSize() {
112:                if (fileObject != null) {
113:                    // Calculate the size of the FileObject
114:                    byte[] data = fileObject.getData();
115:                    long fileSize = data.length;
116:
117:                    // Put this in kilobyte, don't forget to
118:                    // add one on it
119:                    long result = (fileSize / 1024) + 1;
120:
121:                    return result;
122:                } else {
123:                    // No file
124:                    return 0;
125:                }
126:            }
127:
128:            /**
129:             * Get the file.
130:             */
131:            public FileObjectWrapper getFileObject() {
132:                return fileObject;
133:            }
134:
135:            /**
136:             * Set the file.
137:             */
138:            public void setFileObject(FileObjectWrapper fileObject) {
139:                this .fileObject = fileObject;
140:            }
141:
142:            /**
143:             * Calculate the posted data.
144:             */
145:            protected void calculateData(HttpPresentationComms comms)
146:                    throws Exception {
147:                // Get the content type first
148:                String contentType = comms.request.getContentType();
149:
150:                if (contentType == null) {
151:                    contentType = "";
152:                }
153:
154:                // Create the content header
155:                ContentHeader contentHdr = new ContentHeader("Content-Type: "
156:                        + contentType);
157:
158:                // Get the input stream
159:                HttpPresentationInputStream input = comms.request
160:                        .getInputStream();
161:
162:                // Create the multiple mime
163:                MultipartMimeInput mime = new MultipartMimeInput(input,
164:                        contentHdr);
165:                MultipartMimeInputStream in;
166:
167:                // Go through the multiple content...
168:                while ((in = mime.nextPart()) != null) {
169:                    MimeHeader hdr = in.getHeader("Content-Disposition");
170:
171:                    // Check the header
172:                    if (hdr != null) {
173:                        String contentName = null;
174:
175:                        ContentHeader chdr = new ContentHeader(hdr);
176:                        contentName = chdr.getParameter("name");
177:
178:                        // --- Get the file ---
179:                        if (contentName.equals(FILE_FILE)) {
180:                            /* !!!For testing purpose: Directly writing the file to the
181:                               file system!!!
182:                            int n;
183:                            byte[] buffer = new byte[1024];
184:                            String outFileName = "c:/temp/" + contentName +
185:                              System.currentTimeMillis();
186:                                                                                                   
187:                            FileOutputStream out = new FileOutputStream(outFileName);
188:                                                                                                   
189:                            while ((n = in.read(buffer, 0, buffer.length)) > 0) {
190:                              out.write(buffer, 0, n);
191:                            }
192:                                                                                                   
193:                            out.close();
194:                             */
195:
196:                            // Read the stream
197:                            int n;
198:                            byte[] buffer = new byte[1024];
199:
200:                            ByteArrayOutputStream outByte = new ByteArrayOutputStream();
201:
202:                            while ((n = in.read(buffer, 0, buffer.length)) > 0) {
203:                                outByte.write(buffer, 0, n);
204:                            }
205:
206:                            // Save into the FileObject
207:                            outByte.flush();
208:
209:                            byte[] data = outByte.toByteArray();
210:
211:                            // Save into the FileObject, if the size bigger than zero
212:                            if (outByte.size() > 0) {
213:                                fileObject = new FileObjectWrapper(data);
214:                            }
215:
216:                            outByte.close();
217:                        }
218:
219:                        // --- Get the title ---
220:                        if (contentName.equals(FILE_TITLE)) {
221:                            int n;
222:                            byte[] buffer = new byte[1024];
223:
224:                            StringBuffer sb = new StringBuffer();
225:
226:                            while ((n = in.read(buffer, 0, buffer.length)) > 0) {
227:                                for (int i = 0; i < n; i++) {
228:                                    sb
229:                                            .append((char) (((int) (buffer[i]) + 0x100) & 0xff));
230:                                }
231:                            }
232:
233:                            // Save in the private variable
234:                            title = new String(sb);
235:                        }
236:
237:                        // --- Get the date ---
238:                        if (contentName.equals(FILE_DATE)) {
239:                            int n;
240:                            byte[] buffer = new byte[1024];
241:
242:                            StringBuffer sb = new StringBuffer();
243:
244:                            while ((n = in.read(buffer, 0, buffer.length)) > 0) {
245:                                for (int i = 0; i < n; i++) {
246:                                    sb
247:                                            .append((char) (((int) (buffer[i]) + 0x100) & 0xff));
248:                                }
249:                            }
250:
251:                            // Save in the private variable
252:                            date = new String(sb);
253:                        }
254:
255:                        // --- Get the filename ---
256:                        if (contentName.equals(FILE_NAME)) {
257:                            int n;
258:                            byte[] buffer = new byte[1024];
259:
260:                            StringBuffer sb = new StringBuffer();
261:
262:                            while ((n = in.read(buffer, 0, buffer.length)) > 0) {
263:                                for (int i = 0; i < n; i++) {
264:                                    sb
265:                                            .append((char) (((int) (buffer[i]) + 0x100) & 0xff));
266:                                }
267:                            }
268:
269:                            // Save in the private variable
270:                            filename = new String(sb);
271:                        }
272:                    }
273:
274:                    // Close the input but not the whole upload
275:                    in.close();
276:                }
277:            }
278:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.