KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > multipart > UploadPart


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.multipart;
16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 import org.apache.commons.fileupload.FileItem;
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.util.Defense;
24 import org.apache.tapestry.Tapestry;
25 import org.apache.tapestry.request.IUploadFile;
26
27 /**
28  * Portion of a multi-part request representing an uploaded file.
29  *
30  * @author Joe Panico
31  * @since 2.0.1
32  */

33 public class UploadPart extends Object JavaDoc implements IUploadFile
34 {
35     private FileItem _fileItem;
36
37     public UploadPart(FileItem fileItem)
38     {
39         Defense.notNull(fileItem, "fileItem");
40
41         _fileItem = fileItem;
42     }
43
44     public String JavaDoc getContentType()
45     {
46         return _fileItem.getContentType();
47     }
48
49     /**
50      * Leverages {@link File}to convert the full file path and extract the name.
51      */

52     public String JavaDoc getFileName()
53     {
54         File JavaDoc file = new File JavaDoc(this.getFilePath());
55
56         return file.getName();
57     }
58
59     /**
60      * @since 2.0.4
61      */

62
63     public String JavaDoc getFilePath()
64     {
65         return _fileItem.getName();
66     }
67
68     public InputStream JavaDoc getStream()
69     {
70         try
71         {
72             return _fileItem.getInputStream();
73         }
74         catch (IOException JavaDoc ex)
75         {
76             throw new ApplicationRuntimeException(MultipartMessages.unableToOpenContentFile(
77                     this,
78                     ex), ex);
79         }
80     }
81
82     /**
83      * Deletes the external content file, if one exists.
84      */

85
86     public void cleanup()
87     {
88         _fileItem.delete();
89     }
90
91     /**
92      * Writes the uploaded content to a file. This should be invoked at most once (perhaps we should
93      * add a check for this). This will often be a simple file rename.
94      *
95      * @since 3.0
96      */

97     public void write(File JavaDoc file)
98     {
99         try
100         {
101             _fileItem.write(file);
102         }
103         catch (Exception JavaDoc ex)
104         {
105             throw new ApplicationRuntimeException(Tapestry.format(
106                     "UploadPart.write-failure",
107                     file,
108                     ex.getMessage()), ex);
109         }
110     }
111
112     /**
113      * @since 3.0
114      */

115     public long getSize()
116     {
117         return _fileItem.getSize();
118     }
119
120     /**
121      * @since 3.0
122      */

123     public boolean isInMemory()
124     {
125         return _fileItem.isInMemory();
126     }
127
128 }
Popular Tags