KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > upload > DiskFile


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

18
19 package org.apache.struts.upload;
20
21 import java.io.File JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27
28 /**
29  * @deprecated Use the Commons FileUpload based multipart handler instead. This
30  * class will be removed after Struts 1.2.
31  */

32 public class DiskFile implements FormFile {
33     
34     /**
35      * The filepath to the temporary file
36      */

37     protected String JavaDoc filePath;
38     
39     /**
40      * The content type of the file
41      */

42     protected String JavaDoc contentType;
43     
44     /**
45      * The size in bytes of the file
46      */

47     protected int fileSize;
48     
49     /**
50      * The name of the file
51      */

52     protected String JavaDoc fileName;
53     
54     public DiskFile(String JavaDoc filePath) {
55         this.filePath = filePath;
56     }
57     
58     /**
59      * Attempt to read the temporary file and get it's data in byte
60      * array form. Tries to read the entire file (using a byte array
61      * the size of getFileSize()) at once, in one call to FileInputStream.read(byte[]).
62      * For buffered reading, see {@link #getFileData(int) getFileData(int)}.
63      * Note that this method can be dangerous, and that the size of a file
64      * can cause an OutOfMemoryError quite easily. You should use
65      * {@link #getInputStream() getInputStream} and do your own thing.
66      *
67      * @exception FileNotFoundException If the temp file no longer exists
68      * @exception IOException if there is some sort of IO problem.
69      * @see #getFileData(int)
70      */

71     public byte[] getFileData() throws FileNotFoundException JavaDoc, IOException JavaDoc {
72         
73         byte[] bytes = new byte[getFileSize()];
74         
75         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(filePath);
76         fis.read(bytes);
77         fis.close();
78         return bytes;
79     }
80     
81     /**
82      * Attempts to read a file n bytes at a time, n being equal to "bufferSize".
83      * Note that this method can be dangerous, and that the size of a file
84      * can cause an OutOfMemoryError quite easily. You should use
85      * {@link #getInputStream() getInputStream} and do your own thing.
86      *
87      * @param bufferSize The size in bytes that are read from the file at a time
88      * @exception FileNotFoundException If the temp file no longer exists
89      */

90     public byte[] getFileData(int bufferSize) throws FileNotFoundException JavaDoc, IOException JavaDoc {
91         
92         ByteArrayOutputStream JavaDoc byteStream = new ByteArrayOutputStream JavaDoc();
93         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(filePath);
94         
95         int readLength = 0;
96         int totalLength = 0;
97         int offset = 0;
98         
99         byte[] bytes = new byte[bufferSize];
100         
101         while ((readLength = fis.read(bytes, offset, bufferSize)) != -1) {
102             
103             byteStream.write(bytes, offset, bufferSize);
104             totalLength += readLength;
105             offset += readLength;
106         }
107         
108         bytes = byteStream.toByteArray();
109         
110         fis.close();
111         byteStream.close();
112         
113         return bytes;
114     }
115     
116     
117     /**
118      * Delete the temporary file.
119      */

120     public void destroy() {
121         
122         File JavaDoc tempFile = new File JavaDoc(filePath);
123         
124         if (tempFile.exists()) {
125             tempFile.delete();
126         }
127     }
128     
129     /**
130      * Get the temporary file path for this form file
131      * @return A filepath to the temporary file
132      */

133     public String JavaDoc getFilePath() {
134         return filePath;
135     }
136     
137     /**
138      * Set the file name
139      */

140     public void setFileName(String JavaDoc filename) {
141         this.fileName = filename;
142     }
143     
144     /**
145      * Set the content type
146      */

147     public void setContentType(String JavaDoc contentType) {
148         this.contentType = contentType;
149     }
150     
151     /**
152      * Set the file size
153      * @param fileSize The size of the file in bytes
154      */

155     public void setFileSize(int fileSize) {
156         this.fileSize = fileSize;
157     }
158     
159     /**
160      * Get the file name
161      */

162     public String JavaDoc getFileName() {
163         return fileName;
164     }
165     
166     /**
167      * Get the content type
168      */

169     public String JavaDoc getContentType() {
170         return contentType;
171     }
172     
173     /**
174      * Get the file size
175      * @return The size of this file in bytes
176      */

177     public int getFileSize() {
178         return fileSize;
179     }
180     
181     /**
182      * Returns a FileInputStream to the file
183      */

184     public InputStream JavaDoc getInputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc {
185         return new FileInputStream JavaDoc(filePath);
186     }
187 }
188
Popular Tags