1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.util;
25
26 import org.apache.commons.fileupload.FileItem;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32
33
34 /***
35 * A <code>FileHolder</code> holds data saved from a
36 * com.oreilly.servlet.multipart.FilePart either as byteArray [now] or as
37 * reference to temp-file[future versions]
38 *
39 * @author Joe Peer
40 */
41 public class FileHolder implements java.io.Serializable {
42
43 private FileItem fileItem;
44 private String fileName;
45
46 /***
47 * Constructor - takes descriptive data (fileName, contentType) -
48 * inputstream, coming from servletinputstream - we must read it out _now_
49 * - toMemory: true->write it to memory (implemented), false->write it to
50 * tempfile (not implemented yet)
51 *
52 * @param fileName DOCUMENT ME!
53 * @param fileItem DOCUMENT ME!
54 *
55 * @throws IOException DOCUMENT ME!
56 * @throws IllegalArgumentException DOCUMENT ME!
57 */
58 public FileHolder(String fileName,
59 FileItem fileItem)
60 throws IOException, IllegalArgumentException {
61 this.fileName = fileName;
62 this.fileItem = fileItem;
63 }
64
65 /***
66 * Returns the content type of the file data contained within.
67 *
68 * @return content type of the file data.
69 */
70 public String getContentType() {
71 return fileItem.getContentType();
72 }
73
74
75 /***
76 * <p>
77 * Returns the length of the file representated by this FileHolder
78 * </p>
79 *
80 * <p>
81 * fileLength gets determinated either during "bufferInMemory" or
82 * "bufferInFile" (not impl. yet)
83 * </p>
84 *
85 * @return content type of the file data.
86 */
87 public int getFileLength() {
88 return (int) fileItem.getSize();
89 }
90
91
92 /***
93 * Set the name of the file.
94 *
95 * @param fileName DOCUMENT ME!
96 */
97 public void setFileName(String fileName) {
98 this.fileName = fileName;
99 }
100
101
102 /***
103 * Returns the name that the file was stored with on the remote system, or
104 * <code>null</code> if the user didn't enter a file to be uploaded. Note:
105 * this is not the same as the name of the form parameter used to transmit
106 * the file; that is available from the <code>getName</code> method.
107 *
108 * @return name of file uploaded or <code>null</code>.
109 *
110 * @see Part#getName()
111 */
112 public String getFileName() {
113 return fileName;
114 }
115
116
117 /***
118 * DOCUMENT ME!
119 *
120 * @return DOCUMENT ME!
121 *
122 * @throws IOException DOCUMENT ME!
123 */
124 public InputStream getInputStreamFromBuffer() throws IOException {
125 return fileItem.getInputStream();
126 }
127
128
129 /***
130 * Writes out the the file in memory to disk.
131 *
132 * @param fileOrDirectory The file, or directory to write the file to. If
133 * it is a directory, you must provide a file already.
134 *
135 * @throws IOException Thrown if there are rpoblems writing the file.
136 */
137 public void writeBufferToFile(File fileOrDirectory)
138 throws IOException {
139
140 if (fileName != null) {
141
142 File file;
143
144 if (fileOrDirectory.isDirectory()) {
145
146
147 file = new File(fileOrDirectory, fileName);
148 } else {
149
150
151 file = fileOrDirectory;
152 }
153
154 try {
155 fileItem.write(file);
156 } catch (Exception e) {
157 throw new IOException(e.getMessage());
158 }
159 }
160 }
161 }