KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > Upload


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.module;
12
13 import java.util.*;
14 import java.io.*;
15 import java.net.*;
16
17 import org.mmbase.util.*;
18
19 import org.mmbase.util.logging.*;
20
21 /**
22  * the Upload module stores files that are uploaded.
23  * At this time files van only be stored in memory.
24  * nog wat uitleg over hoe je de .shtml maakt
25  */

26 public class Upload extends ProcessorModule {
27
28     private static Logger log = Logging.getLoggerInstance(Upload.class.getName());
29     private Hashtable FilesInMemory = new Hashtable();
30     private String JavaDoc fileUploadDirectory = null;
31
32     public void init() {
33         fileUploadDirectory=getInitParameter("fileUploadDirectory");
34         log.debug("Upload module, fileUploadDirectory = "+fileUploadDirectory);
35     }
36
37     /**
38      * handle the uploaded bytestream.
39      */

40     public boolean process(scanpage sp, Hashtable cmds, Hashtable vars) {
41
42         // Get the place where to store the file
43
// Currently implemented places are: mem:// and file://
44
String JavaDoc filename = (String JavaDoc)cmds.get("file");
45
46         // Get the posted bytearray
47
byte[] bytes = null;
48         try {
49             bytes = sp.poster.getPostParameterBytes("file");
50         } catch (Exception JavaDoc e) {
51             log.error("Upload module postValue to large");
52             return false;
53         }
54
55         if (sp.poster.getPostParameter("file_name") == null) {
56             log.error("no filename ");
57             return false;
58         }
59
60         log.debug("Upload module is storing "+filename);
61         // Store in memory
62
if(filename.indexOf("mem://")!=-1) {
63
64             // Create file object in memory
65
FileInfo fi = new FileInfo();
66             fi.bytes= bytes;
67             fi.name = sp.poster.getPostParameter("file_name");
68             fi.type = sp.poster.getPostParameter("file_type");
69             fi.size = sp.poster.getPostParameter("file_size");
70             FilesInMemory.put(filename,fi);
71             log.debug("Upload module saves in memory: "+fi.toString());
72             return true;
73         }
74         // Store at filesystem
75
if(filename.indexOf("file://")!=-1) {
76             String JavaDoc fname = filename.substring(7);
77             // If no filename is given the real filename will be used.
78
if (fname.equals("")) {
79                 fname = sp.poster.getPostParameter("file_name");
80             }
81             if (fname.indexOf("..")<0) {
82                 saveFile(fileUploadDirectory+fname,bytes);
83                 log.debug("Upload module saved to disk: "+filename);
84                 return true;
85             } else {
86                 log.error("Upload Filename may not contain '..'");
87                 return false;
88             }
89         }
90         return false;
91     }
92
93     /**
94      * deletes an uploaded file.
95      * @param filename the name of the file, e.g. mem://filename
96      */

97     public void deleteFile(String JavaDoc filename) {
98         // Is file located in memory?
99
if(filename.indexOf("mem://")!=-1) {
100             if(FilesInMemory.containsKey(filename)) {
101                 FilesInMemory.remove(filename);
102             }
103         }
104     }
105
106     /**
107      * gets the bytearray of an uploaded file.
108      * @param filename the name of the file, e.g. mem://filename
109      */

110     public byte[] getFile(String JavaDoc filename) {
111         log.debug("Upload module is getting "+filename);
112         // Is file located in memory?
113
if(filename.indexOf("mem://")!=-1) {
114             if(FilesInMemory.containsKey(filename)) {
115                 FileInfo fi = (FileInfo)FilesInMemory.get(filename);
116                 return fi.bytes;
117             }
118         }
119         if(filename.indexOf("http://")!=-1) {
120             return getHttp(filename);
121         }
122         return null;
123     }
124
125
126
127     /**
128      * save bytearray to filesystem
129      * @param filename name of the file
130      * @param value the actual bytes you want to save
131      */

132     private boolean saveFile(String JavaDoc filename,byte[] value) {
133         File file = new File(filename);
134         try {
135             FileOutputStream outputstream = new FileOutputStream(file);
136             outputstream.write(value);
137             outputstream.flush();
138             outputstream.close();
139         } catch(Exception JavaDoc e) {
140             e.printStackTrace();
141             return(false);
142         }
143         return(true);
144     }
145
146
147     /**
148      * this method gets the requested http page
149      */

150     private byte[] getHttp(String JavaDoc page) {
151         URL pageToGrab=null;
152         DataInputStream dis=null;
153
154         try {
155             pageToGrab = new URL(page);
156             dis = new DataInputStream(pageToGrab.openStream());
157         } catch (Exception JavaDoc e) {
158             log.debug("Upload module cannot get page "+page);
159             return null;
160         }
161         StringBuffer JavaDoc tekst= new StringBuffer JavaDoc();
162         String JavaDoc get="";
163         while (true) {
164             try {
165                 get = dis.readLine();
166                 if (get==null) break;
167                 tekst.append(get);
168             } catch (IOException io) {
169                 break;
170             }
171         }
172         return tekst.toString().getBytes();
173     }
174
175
176         /*
177          * a class to store an uploaded file into memory
178          */

179     class FileInfo {
180         byte[] bytes= null;
181         String JavaDoc name = null;
182         String JavaDoc type = null;
183         String JavaDoc size = null;
184
185         public String JavaDoc toString() {
186             return "name="+name+" type="+type+" size="+size;
187         }
188     }
189 }
190
Popular Tags