1 package jodd.servlet; 2 3 import java.io.IOException; 4 5 import jodd.file.FileUtil; 6 7 14 public class UploadedFile { 15 16 private boolean isvalid = true; 17 18 24 public boolean isValid() { 25 return isvalid; 26 } 27 28 31 public UploadedFile() { 32 isvalid = true; 33 } 34 35 41 public UploadedFile(boolean valid) { 42 isvalid = valid; 43 } 44 45 47 private String fieldName; 48 53 public void setFieldName(String v) { 54 fieldName = v; 55 } 56 62 public String getFieldName() { 63 return fieldName; 64 } 65 66 67 private String fileName; 68 76 private void setFileName(String v) { 77 fileName = v; 78 if (fileName == null) { 79 fileExt = null; 80 return; 81 } 82 int start = fileName.lastIndexOf('.') + 1; 83 if (start == -1) { 84 fileExt = ""; 85 return; 86 } 87 fileExt = fileName.substring(start); 88 } 89 94 public String getFileName() { 95 return fileName; 96 } 97 98 private String fileExt; 99 104 public String getFileExt() { 105 return fileExt; 106 } 107 108 109 private String filePathName; 110 115 public void setFilePathName(String v) { 116 filePathName = v; 117 if (filePathName == null) { 118 setFileName(null); 119 return; 120 } 121 int pos = filePathName.lastIndexOf('/'); 122 if (pos != -1) { 123 setFileName(filePathName.substring(pos + 1)); 124 return; 125 } 126 pos = filePathName.lastIndexOf('\\'); 127 if (pos != -1) { 128 setFileName(filePathName.substring(pos + 1)); 129 return; 130 } 131 setFileName(filePathName); 132 } 133 134 139 public String getFilePathName() { 140 return filePathName; 141 } 142 143 144 146 private String contentType; 147 152 public void setContentType(String v) { 153 contentType = v; 154 typeMIME = getTypeMIME(contentType); 155 subTypeMIME = getSubTypeMIME(contentType); 156 } 157 162 public String getContentType() { 163 return contentType; 164 } 165 private String typeMIME; 166 171 public String getTypeMIME() { 172 return typeMIME; 173 } 174 private String subTypeMIME; 175 180 public String getSubTypeMIME() { 181 return subTypeMIME; 182 } 183 184 185 private String contentDisp; 186 191 public void setContentDisp(String v) { 192 contentDisp = v; 193 } 194 199 public String getContentDisp() { 200 return contentDisp; 201 } 202 203 205 private int size; 206 211 public void setSize(int v) { 212 if (v < 0) { 213 v = 0; 214 } 215 size = v; 216 } 217 222 public int getSize() { 223 return size; 224 } 225 226 private int startData; 227 232 public void setDataStart(int v) { 233 if (v < 0) { 234 v = 0; 235 } 236 startData = v; 237 } 238 243 public int getDataStart() { 244 return startData; 245 } 246 247 252 public int getDataEnd() { 253 return startData + size - 1; 254 } 255 256 257 259 private byte[] requestData; 260 261 268 public void setRequestData(byte[] v) { 269 requestData = v; 270 } 271 276 public byte[] getRequestData() { 277 return requestData; 278 } 279 280 285 public byte[] getData() { 286 if (size < 0) { 287 return null; 288 } 289 if (requestData == null) { 290 return null; 291 } 292 byte[] result = new byte[size]; 293 System.arraycopy(requestData, startData, result, 0, size); 294 return result; 295 } 296 297 304 public void writeData(String fileName) throws IOException { 305 FileUtil.writeBytes(fileName, requestData, startData, size); 306 307 } 308 309 310 312 313 private String getTypeMIME(String ContentType) { 314 int pos = ContentType.indexOf("/"); 315 if (pos == -1) { 316 return ContentType; 317 } 318 return ContentType.substring(1, pos); 319 } 320 321 private String getSubTypeMIME(String ContentType) { 322 String value = new String(); 323 int start = ContentType.indexOf("/"); 324 if (start == -1) { 325 return ContentType; 326 } 327 start++; 328 return ContentType.substring(start); 329 } 330 331 333 334 public String toString() { 335 return "UploadedFile: valid=" + isvalid + " fieldName=" + fieldName + " fileName=" + fileName; 336 } 337 338 } 339 | Popular Tags |