01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.upload.services;
16:
17: import org.apache.commons.fileupload.FileItem;
18: import org.apache.commons.io.FilenameUtils;
19:
20: import java.io.File;
21: import java.io.IOException;
22: import java.io.InputStream;
23:
24: /**
25: * Implentation of {@link UploadedFile} for FileItems.
26: */
27: public class UploadedFileItem implements UploadedFile {
28: private final FileItem _item;
29:
30: public UploadedFileItem(FileItem item) {
31: _item = item;
32: }
33:
34: public String getContentType() {
35: return _item.getContentType();
36: }
37:
38: public String getFileName() {
39: return FilenameUtils.getName(getFilePath());
40: }
41:
42: public String getFilePath() {
43: return _item.getName();
44: }
45:
46: public long getSize() {
47: return _item.getSize();
48: }
49:
50: public InputStream getStream() {
51: try {
52: return _item.getInputStream();
53: } catch (IOException e) {
54: throw new RuntimeException(UploadMessages
55: .unableToOpenContentFile(this ), e);
56: }
57: }
58:
59: public boolean isInMemory() {
60: return _item.isInMemory();
61: }
62:
63: public void write(File file) {
64: try {
65: _item.write(file);
66: } catch (Exception e) {
67: throw new RuntimeException(UploadMessages
68: .writeFailure(file), e);
69: }
70: }
71:
72: public void cleanup() {
73: _item.delete();
74: }
75: }
|