1 package org.apache.turbine.util.upload; 2 3 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedOutputStream ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.UnsupportedEncodingException ; 30 import javax.activation.DataSource ; 31 32 import org.apache.turbine.services.uniqueid.TurbineUniqueId; 33 import org.apache.turbine.services.upload.TurbineUpload; 34 35 61 public class FileItem implements DataSource 62 { 63 67 public static final int DEFAULT_UPLOAD_SIZE_THRESHOLD = 10240; 68 69 70 protected String fileName; 71 72 76 protected String contentType; 77 78 79 protected byte[] content; 80 81 82 protected File storeLocation; 83 84 85 protected ByteArrayOutputStream byteStream; 86 87 97 protected FileItem(String fileName, String contentType) 98 { 99 this.fileName = fileName; 100 this.contentType = contentType; 101 } 102 103 109 public String getName() 110 { 111 return getFileName(); 112 } 113 114 119 public String getFileName() 120 { 121 return fileName; 122 } 123 124 132 public String getContentType() 133 { 134 return contentType; 135 } 136 137 143 public boolean inMemory() 144 { 145 return (content != null || byteStream != null); 146 } 147 148 153 public long getSize() 154 { 155 if (storeLocation != null) 156 { 157 return storeLocation.length(); 158 } 159 else if (byteStream != null) 160 { 161 return byteStream.size(); 162 } 163 else 164 { 165 return content.length; 166 } 167 } 168 169 176 public byte[] get() 177 { 178 if (content == null) 179 { 180 if (storeLocation != null) 181 { 182 content = new byte[(int) getSize()]; 183 try 184 { 185 FileInputStream fis = new FileInputStream (storeLocation); 186 fis.read(content); 187 } 188 catch (Exception e) 189 { 190 content = null; 191 } 192 } 193 else 194 { 195 content = byteStream.toByteArray(); 196 byteStream = null; 197 } 198 } 199 return content; 200 } 201 202 209 public String getString() 210 { 211 return new String (get()); 212 } 213 214 223 public String getString(String encoding) 224 throws UnsupportedEncodingException 225 { 226 return new String (get(), encoding); 227 } 228 229 238 public InputStream getInputStream() 239 throws IOException 240 { 241 return getStream(); 242 } 243 244 252 public InputStream getStream() 253 throws IOException 254 { 255 if (content == null) 256 { 257 if (storeLocation != null) 258 { 259 return new FileInputStream (storeLocation); 260 } 261 else 262 { 263 content = byteStream.toByteArray(); 264 byteStream = null; 265 } 266 } 267 return new ByteArrayInputStream (content); 268 } 269 270 282 public File getStoreLocation() 283 { 284 return storeLocation; 285 } 286 287 290 protected void finalize() 291 { 292 if (storeLocation != null && storeLocation.exists()) 293 { 294 storeLocation.delete(); 295 } 296 } 297 298 307 public OutputStream getOutputStream() 308 throws IOException 309 { 310 if (storeLocation == null) 311 { 312 return byteStream; 313 } 314 else 315 { 316 return new FileOutputStream (storeLocation); 317 } 318 } 319 320 336 public static FileItem newInstance(String path, 337 String name, 338 String contentType, 339 int requestSize) 340 { 341 FileItem item = new FileItem(name, contentType); 342 if (requestSize > TurbineUpload.getSizeThreshold()) 343 { 344 String instanceName = TurbineUniqueId.getInstanceId(); 345 String fileName = TurbineUniqueId.getUniqueId(); 346 fileName = instanceName + "_upload_" + fileName + ".tmp"; 347 fileName = path + "/" + fileName; 348 item.storeLocation = new File (fileName); 349 item.storeLocation.deleteOnExit(); 350 } 351 else 352 { 353 item.byteStream = new ByteArrayOutputStream (); 354 } 355 return item; 356 } 357 358 368 public void write(String file) throws Exception 369 { 370 if (inMemory()) 371 { 372 FileOutputStream fout = null; 373 try 374 { 375 fout = new FileOutputStream (file); 376 fout.write(get()); 377 } 378 finally 379 { 380 if (fout != null) 381 { 382 fout.close(); 383 } 384 } 385 } 386 else if (storeLocation != null) 387 { 388 393 if (storeLocation.renameTo(new File (file)) == false) 394 { 395 BufferedInputStream in = null; 396 BufferedOutputStream out = null; 397 try 398 { 399 in = new BufferedInputStream ( 400 new FileInputStream (storeLocation)); 401 out = new BufferedOutputStream (new FileOutputStream (file)); 402 byte[] bytes = new byte[2048]; 403 int s = 0; 404 while ((s = in.read(bytes)) != -1) 405 { 406 out.write(bytes, 0, s); 407 } 408 } 409 finally 410 { 411 try 412 { 413 in.close(); 414 } 415 catch (Exception e) 416 { 417 } 419 try 420 { 421 out.close(); 422 } 423 catch (Exception e) 424 { 425 } 427 } 428 } 429 } 430 else 431 { 432 436 throw new Exception ("Cannot write uploaded file to disk!"); 437 } 438 } 439 } 440 | Popular Tags |