1 15 package org.apache.tapestry.multipart; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 21 import org.apache.commons.fileupload.FileItem; 22 import org.apache.hivemind.ApplicationRuntimeException; 23 import org.apache.hivemind.util.Defense; 24 import org.apache.tapestry.Tapestry; 25 import org.apache.tapestry.request.IUploadFile; 26 27 33 public class UploadPart extends Object implements IUploadFile 34 { 35 private FileItem _fileItem; 36 37 public UploadPart(FileItem fileItem) 38 { 39 Defense.notNull(fileItem, "fileItem"); 40 41 _fileItem = fileItem; 42 } 43 44 public String getContentType() 45 { 46 return _fileItem.getContentType(); 47 } 48 49 52 public String getFileName() 53 { 54 File file = new File (this.getFilePath()); 55 56 return file.getName(); 57 } 58 59 62 63 public String getFilePath() 64 { 65 return _fileItem.getName(); 66 } 67 68 public InputStream getStream() 69 { 70 try 71 { 72 return _fileItem.getInputStream(); 73 } 74 catch (IOException ex) 75 { 76 throw new ApplicationRuntimeException(MultipartMessages.unableToOpenContentFile( 77 this, 78 ex), ex); 79 } 80 } 81 82 85 86 public void cleanup() 87 { 88 _fileItem.delete(); 89 } 90 91 97 public void write(File file) 98 { 99 try 100 { 101 _fileItem.write(file); 102 } 103 catch (Exception ex) 104 { 105 throw new ApplicationRuntimeException(Tapestry.format( 106 "UploadPart.write-failure", 107 file, 108 ex.getMessage()), ex); 109 } 110 } 111 112 115 public long getSize() 116 { 117 return _fileItem.getSize(); 118 } 119 120 123 public boolean isInMemory() 124 { 125 return _fileItem.isInMemory(); 126 } 127 128 } | Popular Tags |