1 64 65 package com.jcorporate.expresso.core.misc.upload; 66 67 import com.jcorporate.expresso.core.misc.StringUtil; 68 import org.apache.struts.upload.FormFile; 69 70 import javax.activation.DataSource ; 71 import java.io.ByteArrayInputStream ; 72 import java.io.ByteArrayOutputStream ; 73 import java.io.File ; 74 import java.io.FileInputStream ; 75 import java.io.FileNotFoundException ; 76 import java.io.FileOutputStream ; 77 import java.io.IOException ; 78 import java.io.InputStream ; 79 import java.io.OutputStream ; 80 import java.io.UnsupportedEncodingException ; 81 82 83 102 public class FileItem 103 implements FormFile, DataSource { 104 105 109 public static final int DEFAULT_UPLOAD_SIZE_THRESHOLD = 10240; 110 111 114 protected String fileName; 115 116 120 protected String contentType; 121 122 125 protected byte[] content; 126 127 130 protected File storeLocation = null; 131 132 135 protected ByteArrayOutputStream byteStream; 136 137 147 protected FileItem(String fileName, String contentType) { 148 this.fileName = fileName; 149 this.contentType = contentType; 150 } 151 152 157 public String getFileName() { 158 return fileName; 159 } 160 161 168 public String getContentType() { 169 return contentType; 170 } 171 172 178 public boolean inMemory() { 179 return (content != null || byteStream != null); 180 } 181 182 187 public long getSize() { 188 if (storeLocation != null) { 189 return storeLocation.length(); 190 } else if (byteStream != null) { 191 return byteStream.size(); 192 } else { 193 return content.length; 194 } 195 } 196 197 204 public byte[] get() { 205 if (content == null) { 206 if (storeLocation != null) { 207 content = new byte[(int) getSize()]; 208 209 try { 210 FileInputStream fis = new FileInputStream (storeLocation); 211 fis.read(content); 212 } catch (Exception e) { 213 content = null; 214 } 215 } else { 216 content = byteStream.toByteArray(); 217 byteStream = null; 218 } 219 } 220 221 return content; 222 } 223 224 231 public String getString() { 232 return new String (get()); 233 } 234 235 243 public String getString(String encoding) 244 throws UnsupportedEncodingException { 245 return new String (get(), encoding); 246 } 247 248 256 public InputStream getStream() 257 throws Exception { 258 if (content == null) { 259 if (storeLocation != null) { 260 try { 261 return new FileInputStream (storeLocation); 262 } catch (FileNotFoundException e) { 263 throw new Exception ("FileItem: stored item was lost"); 264 } 265 } else { 266 content = byteStream.toByteArray(); 267 byteStream = null; 268 } 269 } 270 271 return new ByteArrayInputStream (content); 272 } 273 274 286 public File getStoreLocation() { 287 return storeLocation; 288 } 289 290 297 public OutputStream getOutputStream() 298 throws IOException { 299 if (storeLocation == null) { 300 return byteStream; 301 } else { 302 return new FileOutputStream (storeLocation); 303 } 304 } 305 306 323 public static FileItem newInstance(String path, String name, 324 String contentType, int requestSize, 325 boolean storeAsFile) { 326 FileItem item = new FileItem(name, contentType); 327 328 if (storeAsFile) { 329 330 331 try { 332 333 name = StringUtil.replace(name, "\\", "/"); 337 338 File tmpFile = new File(name); 339 340 File tempDir = new File(path); 343 tempDir.mkdirs(); 344 345 item.storeLocation = File.createTempFile("upload", 348 "-" + 349 tmpFile.getName(), 350 tempDir); 351 352 } catch (IOException de) { 354 throw new IllegalArgumentException ("Unable to create upload temp file " + 355 de.getMessage()); 356 } 357 } else { 358 item.byteStream = new ByteArrayOutputStream (); 359 } 360 361 return item; 362 } 363 364 369 public void setContentType(String contentType) { 370 this.contentType = contentType; 371 } 372 373 378 public int getFileSize() { 379 return new Long (getSize()).intValue(); 380 } 381 382 387 public void setFileSize(int filesize) { 388 389 390 } 391 392 397 public void setFileName(String newFileName) { 398 fileName = newFileName; 399 } 400 401 414 public byte[] getFileData() 415 throws FileNotFoundException , IOException { 416 byte[] b = null; 417 getInputStream().read(b); 418 419 return b; 420 } 421 422 431 public InputStream getInputStream() 432 throws FileNotFoundException , IOException { 433 return new FileInputStream (getFileName()); 434 } 435 436 441 public void destroy() { 442 File theFile = new File(getFileName()); 443 theFile.delete(); 444 } 445 446 451 public String getName() { 452 return this.fileName; 453 } 454 } | Popular Tags |