1 18 19 package org.apache.struts.upload; 20 21 import java.io.File ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.FileInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.FileNotFoundException ; 27 28 32 public class DiskFile implements FormFile { 33 34 37 protected String filePath; 38 39 42 protected String contentType; 43 44 47 protected int fileSize; 48 49 52 protected String fileName; 53 54 public DiskFile(String filePath) { 55 this.filePath = filePath; 56 } 57 58 71 public byte[] getFileData() throws FileNotFoundException , IOException { 72 73 byte[] bytes = new byte[getFileSize()]; 74 75 FileInputStream fis = new FileInputStream (filePath); 76 fis.read(bytes); 77 fis.close(); 78 return bytes; 79 } 80 81 90 public byte[] getFileData(int bufferSize) throws FileNotFoundException , IOException { 91 92 ByteArrayOutputStream byteStream = new ByteArrayOutputStream (); 93 FileInputStream fis = new FileInputStream (filePath); 94 95 int readLength = 0; 96 int totalLength = 0; 97 int offset = 0; 98 99 byte[] bytes = new byte[bufferSize]; 100 101 while ((readLength = fis.read(bytes, offset, bufferSize)) != -1) { 102 103 byteStream.write(bytes, offset, bufferSize); 104 totalLength += readLength; 105 offset += readLength; 106 } 107 108 bytes = byteStream.toByteArray(); 109 110 fis.close(); 111 byteStream.close(); 112 113 return bytes; 114 } 115 116 117 120 public void destroy() { 121 122 File tempFile = new File (filePath); 123 124 if (tempFile.exists()) { 125 tempFile.delete(); 126 } 127 } 128 129 133 public String getFilePath() { 134 return filePath; 135 } 136 137 140 public void setFileName(String filename) { 141 this.fileName = filename; 142 } 143 144 147 public void setContentType(String contentType) { 148 this.contentType = contentType; 149 } 150 151 155 public void setFileSize(int fileSize) { 156 this.fileSize = fileSize; 157 } 158 159 162 public String getFileName() { 163 return fileName; 164 } 165 166 169 public String getContentType() { 170 return contentType; 171 } 172 173 177 public int getFileSize() { 178 return fileSize; 179 } 180 181 184 public InputStream getInputStream() throws FileNotFoundException , IOException { 185 return new FileInputStream (filePath); 186 } 187 } 188 | Popular Tags |