1 3 package jodd.servlet.upload; 4 5 import jodd.io.FileNameUtil; 6 7 10 public class FileUploadHeader { 11 12 String dataHeader; 13 String formFieldName; 14 15 String formFileName; 16 String path; 17 String fileName; 18 19 boolean isFile; 20 String contentType; 21 String mimeType; 22 String mimeSubtype; 23 String contentDisposition; 24 25 26 FileUploadHeader(String dataHeader) { 27 this.dataHeader = dataHeader; 28 isFile = dataHeader.indexOf("filename") > 0; 29 formFieldName = getDataFieldValue(dataHeader, "name"); 30 if (isFile) { 31 formFileName = getDataFieldValue(dataHeader, "filename"); 32 if (formFileName == null) { 33 return; 34 } 35 if (formFileName.length() == 0) { 36 path = ""; 37 fileName = ""; 38 } 39 int ls = FileNameUtil.indexOfLastSeparator(formFileName); 40 if (ls == -1) { 41 path = ""; 42 fileName = formFileName; 43 } else { 44 path = formFileName.substring(0, ls); 45 fileName = formFileName.substring(ls); 46 } 47 if (fileName.length() > 0) { 48 this.contentType = getContentType(dataHeader); 49 mimeType = getMimeType(contentType); 50 mimeSubtype = getMimeSubtype(contentType); 51 contentDisposition = getContentDisposition(dataHeader); 52 } 53 } 54 } 55 56 58 61 private String getDataFieldValue(String dataHeader, String fieldName) { 62 String value = null; 63 String token = String.valueOf((new StringBuffer (String.valueOf(fieldName))).append('=').append('"')); 64 int pos = dataHeader.indexOf(token); 65 if (pos > 0) { 66 int start = pos + token.length(); 67 int end = dataHeader.indexOf('"', start); 68 if ((start > 0) && (end > 0)) { 69 value = dataHeader.substring(start, end); 70 } 71 } 72 return value; 73 } 74 75 80 private String getContentType(String dataHeader) { 81 String token = "Content-Type:"; 82 int start = dataHeader.indexOf(token); 83 if (start == -1) { 84 return ""; 85 } 86 start += token.length(); 87 return dataHeader.substring(start); 88 } 89 90 private String getContentDisposition(String dataHeader) { 91 int start = dataHeader.indexOf(':') + 1; 92 int end = dataHeader.indexOf(';'); 93 return dataHeader.substring(start, end); 94 } 95 96 private String getMimeType(String ContentType) { 97 int pos = ContentType.indexOf('/'); 98 if (pos == -1) { 99 return ContentType; 100 } 101 return ContentType.substring(1, pos); 102 } 103 104 private String getMimeSubtype(String ContentType) { 105 int start = ContentType.indexOf('/'); 106 if (start == -1) { 107 return ContentType; 108 } 109 start++; 110 return ContentType.substring(start); 111 } 112 113 114 116 120 public boolean isFile() { 121 return isFile; 122 } 123 124 127 public String getFormFieldName() { 128 return formFieldName; 129 } 130 131 134 public String getFormFilename() { 135 return formFileName; 136 } 137 138 141 public String getFileName() { 142 return fileName; 143 } 144 145 152 public String getContentType() { 153 return contentType; 154 } 155 156 159 public String getMimeType() { 160 return mimeType; 161 } 162 163 166 public String getMimeSubtype() { 167 return mimeSubtype; 168 } 169 170 173 public String getContentDisposition() { 174 return contentDisposition; 175 } 176 } 177 | Popular Tags |