1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.fileupload;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.Serializable;
26
27 import org.apache.commons.fileupload.FileItem;
28 import org.apache.commons.fileupload.disk.DiskFileItem;
29
30
31
32
33
34
35 public class UploadedFileDefaultFileImpl extends UploadedFileDefaultImplBase
36 {
37 private static final long serialVersionUID = -6401426361519246443L;
38 private transient DiskFileItem fileItem = null;
39 private StorageStrategy storageStrategy;
40
41 public UploadedFileDefaultFileImpl(final FileItem fileItem) throws IOException
42 {
43 super(fileItem.getName(), fileItem.getContentType());
44 this.fileItem = (DiskFileItem) fileItem;
45 storageStrategy = new DefaultDiskStorageStrategy();
46 }
47
48 private class DefaultDiskStorageStrategy
49 extends DiskStorageStrategy implements Serializable
50 {
51 private static final long serialVersionUID = 5191237379179109587L;
52
53 public DefaultDiskStorageStrategy()
54 {
55 }
56
57 public File getTempFile()
58 {
59 if (UploadedFileDefaultFileImpl.this.fileItem != null)
60 {
61 return UploadedFileDefaultFileImpl.this.fileItem.getStoreLocation();
62 }
63 else
64 {
65 return null;
66 }
67 }
68
69 public void deleteFileContents()
70 {
71
72
73
74
75
76 if (UploadedFileDefaultFileImpl.this.fileItem != null)
77 {
78 UploadedFileDefaultFileImpl.this.fileItem.delete();
79 }
80 }
81 }
82
83
84
85
86
87
88 public byte[] getBytes() throws IOException
89 {
90 byte[] bytes = new byte[(int)getSize()];
91 if (fileItem != null) fileItem.getInputStream().read(bytes);
92 return bytes;
93 }
94
95
96
97
98
99
100
101
102 public InputStream getInputStream() throws IOException
103 {
104 return fileItem != null
105 ? fileItem.getInputStream()
106 : new ByteArrayInputStream(new byte[0]);
107 }
108
109
110
111
112
113
114 public long getSize()
115 {
116 return fileItem != null ? fileItem.getSize() : 0;
117 }
118
119
120 public StorageStrategy getStorageStrategy() {
121 return storageStrategy;
122 }
123 }