KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > FileHolder


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/FileHolder.java,v 1.22 2004/10/27 06:41:53 hkollmann Exp $
3  * $Revision: 1.22 $
4  * $Date: 2004/10/27 06:41:53 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.util;
25
26 import org.apache.commons.fileupload.FileItem;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
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 JavaDoc {
42    // buffer to hold data till its use (used if memory buffering choosen)
43
private FileItem fileItem;
44    private String JavaDoc 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 JavaDoc fileName,
59                      FileItem fileItem)
60               throws IOException JavaDoc, IllegalArgumentException JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getInputStreamFromBuffer() throws IOException JavaDoc {
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 JavaDoc fileOrDirectory)
138                           throws IOException JavaDoc {
139       // Only do something if this part contains a file
140
if (fileName != null) {
141          // Check if user supplied directory
142
File JavaDoc file;
143
144          if (fileOrDirectory.isDirectory()) {
145             // Write it to that dir the user supplied,
146
// with the filename it arrived with
147
file = new File JavaDoc(fileOrDirectory, fileName);
148          } else {
149             // Write it to the file the user supplied,
150
// ignoring the filename it arrived with
151
file = fileOrDirectory;
152          }
153
154          try {
155             fileItem.write(file);
156          } catch (Exception JavaDoc e) {
157             throw new IOException JavaDoc(e.getMessage());
158          }
159       }
160    }
161 }
162
Popular Tags