Source Code Cross Referenced for FileUpload.java in  » J2EE » wicket » org » apache » wicket » markup » html » form » upload » 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 » wicket » org.apache.wicket.markup.html.form.upload 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.wicket.markup.html.form.upload;
018:
019:        import java.io.File;
020:        import java.io.IOException;
021:        import java.io.InputStream;
022:        import java.util.ArrayList;
023:        import java.util.Iterator;
024:        import java.util.List;
025:
026:        import org.apache.wicket.IClusterable;
027:        import org.apache.wicket.Session;
028:        import org.apache.wicket.util.file.Files;
029:        import org.apache.wicket.util.upload.FileItem;
030:
031:        /**
032:         * Model for file uploads.
033:         * 
034:         * @author Jonathan Locke
035:         */
036:        public class FileUpload implements  IClusterable {
037:            private static final long serialVersionUID = 1L;
038:
039:            private final FileItem item;
040:
041:            private transient List/* <InputStream> */inputStreamsToClose;
042:
043:            /**
044:             * Constructor
045:             * 
046:             * @param item
047:             *            The uploaded file item
048:             */
049:            public FileUpload(final FileItem item) {
050:                this .item = item;
051:            }
052:
053:            /**
054:             * Close the streams which has been opened when getting the InputStream
055:             * using {@link #getInputStream()}. All the input streams are closed at the
056:             * end of the request. This is done when the FileUploadField, which is
057:             * associated with this FileUpload is detached.
058:             * <p>
059:             * If an exception is thrown when closing the input streams, we ignore it,
060:             * because the stream might have been closed already.
061:             */
062:            public final void closeStreams() {
063:                if (inputStreamsToClose != null) {
064:                    for (Iterator inputStreamsIterator = inputStreamsToClose
065:                            .iterator(); inputStreamsIterator.hasNext();) {
066:                        InputStream inputStream = (InputStream) inputStreamsIterator
067:                                .next();
068:
069:                        try {
070:                            inputStream.close();
071:                        } catch (IOException e) {
072:                            // We don't care aobut the exceptions thrown here.
073:                        }
074:                    }
075:
076:                    // Reset the list
077:                    inputStreamsToClose = null;
078:                }
079:            }
080:
081:            /**
082:             * Deletes temp file from disk
083:             */
084:            public void delete() {
085:                item.delete();
086:            }
087:
088:            /**
089:             * @return Uploaded file as an array of bytes
090:             */
091:            public byte[] getBytes() {
092:                return item.get();
093:            }
094:
095:            /**
096:             * @since 1.2
097:             * @return name of uploaded client side file
098:             */
099:            public String getClientFileName() {
100:                return item.getName();
101:            }
102:
103:            /**
104:             * @return Content type for upload
105:             */
106:            public String getContentType() {
107:                return item.getContentType();
108:            }
109:
110:            /**
111:             * Get an input stream for the file uploaded. Use this input stream if you
112:             * can't use {@link #writeTo(File)} for persisting the uploaded file. This
113:             * can be if you need to react upon the content of the file or need to
114:             * persist it elsewhere, i.e. a database or external filesystem.
115:             * <p>
116:             * <b>PLEASE NOTE!</b><br>
117:             * The InputStream return will be closed be Wicket at the end of the
118:             * request. If you need it across a request you need to hold on to this
119:             * FileUpload instead.
120:             * 
121:             * @return Input stream with file contents.
122:             * @throws IOException
123:             */
124:            public InputStream getInputStream() throws IOException {
125:                if (inputStreamsToClose == null) {
126:                    inputStreamsToClose = new ArrayList/* <InputStream> */();
127:                }
128:
129:                InputStream is = item.getInputStream();
130:                inputStreamsToClose.add(is);
131:
132:                return is;
133:            }
134:
135:            /**
136:             * @return The upload's size
137:             */
138:            public long getSize() {
139:                return item.getSize();
140:            }
141:
142:            /**
143:             * Saves this file upload to a given file on the server side.
144:             * 
145:             * @param file
146:             *            The file
147:             * @throws IOException
148:             */
149:            public void writeTo(final File file) throws IOException {
150:                InputStream is = getInputStream();
151:                try {
152:                    Files.writeTo(file, is);
153:                } finally {
154:                    is.close();
155:                }
156:            }
157:
158:            /**
159:             * Convinience method that copies the input stream returned by
160:             * {@link #getInputStream()} into a temporary file.
161:             * <p>
162:             * Only use this if you actually need a {@link File} to work with, in all
163:             * other cases use {@link #getInputStream()} or {@link #getBytes()}
164:             * 
165:             * @since 1.2
166:             * 
167:             * @return temporary file containing the contents of the uploaded file
168:             * @throws IOException
169:             */
170:            public final File writeToTempFile() throws IOException {
171:                File temp = File.createTempFile(Session.get().getId(), item
172:                        .getFieldName());
173:                writeTo(temp);
174:                return temp;
175:            }
176:        }
w___ww.___j__a_v__a_2___s__.___c__o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.