1 package org.jahia.tools.files; 2 3 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.util.Enumeration ; 12 import java.util.Hashtable ; 13 import java.util.StringTokenizer ; 14 import java.util.Vector ; 15 16 import javax.servlet.ServletContext ; 17 import javax.servlet.http.HttpServletRequest ; 18 19 import com.oreilly.servlet.MultipartRequest; 20 import org.jahia.bin.Jahia; 21 22 31 public class FileUpload { 32 33 private static org.apache.log4j.Logger logger = 34 org.apache.log4j.Logger.getLogger (FileUpload.class); 35 36 private static final String REQ_CONTENT_TYPE = "multipart/form-data"; 37 38 private MultipartRequest m_MultiPartReq = null; 39 private Hashtable m_QueryStringParams; 40 private ServletContext m_Context; 41 private HttpServletRequest m_Req; 42 43 private String m_SavePath = ""; 44 private int m_FileMaxSize = 1024 * 1024; 46 49 public FileUpload (ServletContext context, HttpServletRequest req) 50 throws IOException { 51 52 m_Context = context; 53 m_Req = req; 54 init(); 55 } 56 57 65 public FileUpload (ServletContext context, HttpServletRequest req, 66 String savePath) 67 throws IOException { 68 69 m_Context = context; 70 m_Req = req; 71 m_SavePath = savePath; 72 init(); 73 74 } 75 76 85 public FileUpload (ServletContext context, HttpServletRequest req, 86 String savePath, int fileMaxSize) 87 throws IOException { 88 89 m_Context = context; 90 m_Req = req; 91 m_FileMaxSize = fileMaxSize; 92 m_SavePath = savePath; 93 init(); 94 95 } 96 97 102 protected void init () 103 throws IOException { 104 105 if (m_MultiPartReq == null) { 106 107 if (checkSavePath(m_SavePath)) { 108 109 try { 110 111 String encoding = "ISO-8859-1"; 112 if (m_Req.getCharacterEncoding() == null) { 113 if (Jahia.getSettings() != null) { 114 if (Jahia.getSettings().isUtf8Encoding()) { 115 encoding = "UTF-8"; 116 } else { 117 encoding = "ISO-8859-1"; 118 } 119 } 120 } else { 121 encoding = m_Req.getCharacterEncoding(); 122 } 123 124 m_MultiPartReq = new MultipartRequest(m_Req, m_SavePath, 125 m_FileMaxSize, encoding); 126 127 } catch (IOException ioe) { 128 129 logger.error("Error while initializing FileUpload class:", ioe); 130 throw new IOException (ioe.getMessage()); 131 } 132 133 } else { 134 logger.error( 135 "FileUpload::init storage path does not exists or can write"); 136 throw new IOException ( 137 "FileUpload::init storage path does not exists or cannot write"); 138 } 139 } 140 141 parseQueryString(); 142 143 } 144 145 151 public Vector getFiles () 152 throws IOException { 153 154 logger.debug("fileMaxSize = " + m_FileMaxSize + ", SavePath = " + m_SavePath); 155 156 Enumeration fileNames = m_MultiPartReq.getFileNames(); 157 158 File tmpFile = null; 159 Vector files = new Vector (); 160 161 while (fileNames.hasMoreElements()) { 162 tmpFile = m_MultiPartReq.getFile( (String ) fileNames.nextElement()); 163 if (tmpFile != null) { 164 files.addElement(tmpFile); 165 } 166 } 167 168 return files; 169 } 170 171 177 public Enumeration getFileNames () { 178 179 return m_MultiPartReq.getFileNames(); 180 } 181 182 188 public File getFile (String filename) { 189 190 return m_MultiPartReq.getFile(filename); 191 } 192 193 199 public String getFileContentType (String filename) { 200 201 return m_MultiPartReq.getContentType(filename); 202 } 203 204 210 public String getFileSystemName (String filename) { 211 212 return m_MultiPartReq.getFilesystemName(filename); 213 } 214 215 220 public Enumeration getParameterNames () { 221 222 return m_MultiPartReq.getParameterNames(); 223 } 224 225 231 public String getParameter (String paramName) { 232 233 if (m_MultiPartReq.getParameter(paramName) == null) { 234 return (String ) m_QueryStringParams.get(paramName); 235 } 236 return m_MultiPartReq.getParameter(paramName); 237 } 238 239 245 public String getQueryParameter (String paramName) { 246 247 return (String ) m_QueryStringParams.get(paramName); 248 } 249 250 256 public String [] getParameterValues (String paramName) { 257 258 return m_MultiPartReq.getParameterValues(paramName); 259 } 260 261 268 protected boolean checkSavePath (String path) { 269 270 if (path != null && (path.length() > 0)) { 271 272 logger.debug("path is " + path); 273 File tmpFile = new File (path); 274 if (tmpFile != null && tmpFile.isDirectory() && tmpFile.canWrite()) { 275 m_SavePath = path; 276 return true; 277 } else { 278 return false; 279 } 280 } else { 281 return false; 282 } 283 } 284 285 288 public void deleteUploadFolder () { 289 290 File tmpDir = new File (m_SavePath); 291 if (tmpDir != null && tmpDir.isDirectory() && tmpDir.canWrite()) { 292 tmpDir.delete(); 293 } 294 } 295 296 301 protected void parseQueryString () { 302 303 m_QueryStringParams = new Hashtable (); 304 logger.debug(m_Req.getQueryString()); 305 306 StringTokenizer tokenizer = new StringTokenizer (m_Req.getQueryString(), 307 "&"); 308 Vector params = new Vector (); 309 while (tokenizer.hasMoreTokens()) { 310 params.add(tokenizer.nextToken()); 311 } 312 313 String param = null; 314 int size = params.size(); 315 int pos = 0; 316 for (int i = 0; i < size; i++) { 317 param = (String ) params.get(i); 318 pos = param.indexOf("="); 319 if (pos > 0) { 320 m_QueryStringParams.put(param.substring(0, pos), 321 param.substring(pos + 1, param.length())); 322 } 323 324 } 325 326 } 327 328 } | Popular Tags |