1 22 23 package org.xquark.servlet.util; 24 25 import java.util.HashMap ; 26 import java.io.*; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 30 31 35 public class PostDataParser { 36 static final int PAD_LENGTH = "-----------------------------".length(); 37 38 41 public StringBuffer data; 42 43 public String queryString; 44 45 HashMap parameterMap; 46 50 String RequestID; 51 52 53 56 public PostDataParser(String data) 57 { 58 this.data = new StringBuffer (data); 59 queryString = new String (data); 60 this.parameterMap = new HashMap (); 61 parseRequestID(); 62 parseParameters(); 63 } 64 65 66 public PostDataParser(HttpServletRequest request) 67 throws IOException 68 { 69 ServletInputStream in = request.getInputStream(); 70 this.data = new StringBuffer (); 71 int c; 72 while((c=in.read()) != -1) this.data.append((char)c); 73 74 queryString = new String (data); 75 this.parameterMap = new HashMap (); 79 parseRequestID(); 80 parseParameters(); 81 } 82 83 84 88 void parseRequestID() 89 { 90 this.RequestID = data.substring(PAD_LENGTH, PAD_LENGTH+12); 91 this.data = data.delete(0, PAD_LENGTH + this.RequestID.length()); 92 } 93 94 95 private String getRequestID() 96 { 97 return this.RequestID; 98 } 99 100 101 private void parseParameters() 102 { 103 String s = null; 104 105 while((s=extractParameter()) != null) 106 parseParameter(s); 107 } 108 109 110 private String extractParameter() 111 { 112 int begin = 0; 113 int end = 0; 114 115 begin = data.toString().indexOf("Content-Disposition: form-data; ") + "Content-Disposition: form-data; ".length(); 116 118 119 if(begin == 31) return null; 121 122 end = data.toString().indexOf(RequestID) - PAD_LENGTH; 123 125 String parameter = data.substring(begin, end); 126 127 this.data = this.data.delete(0, end+PAD_LENGTH+this.RequestID.length()+1); 128 return parameter; 130 } 131 132 133 private void parseParameter(String s) 134 { 135 if(s== null) 136 return; 137 138 int begin = s.indexOf("\""); 139 int end = s.indexOf("\"", begin+1); 140 String name = s.substring(begin+1, end).trim(); 142 String value = s.substring(end+2, s.length()-1); 143 144 if(isFileEntry(value)) 145 { 146 this.parameterMap.put(name, parseUploadFile(value)); 147 } 148 else 149 { 150 this.parameterMap.put(name, value.trim()); 151 } 152 } 153 154 boolean isFileEntry(String data) 155 { 156 return data.startsWith(" filename=\""); 157 } 158 159 160 private FileEntry parseUploadFile(String data) 161 { 162 163 int begin = " filename=\"".length(); 164 int end = data.indexOf("\"", begin); 165 166 String filename = data.substring(begin, end); 167 168 begin = data.indexOf("\r\nContent-Type: ", end) + "\r\nContent-Type: ".length(); 169 end = data.indexOf("\r\n", begin); 170 171 String contentType = data.substring(begin, end); 172 String fileContent = data.substring(end+1, data.length()-1); 173 174 175 186 187 return new FileEntry(filename, contentType, fileContent); 188 } 189 190 191 192 199 public String getParameter(String name) 200 { 201 return (String ) this.parameterMap.get(name); 202 } 203 204 205 public String getFileContent(String name) 206 { 207 return ((FileEntry)this.parameterMap.get(name)).getContent(); 208 } 209 210 211 public FileEntry getFileEntry(String name) 212 { 213 return (FileEntry) this.parameterMap.get(name); 214 } 215 216 217 220 public static void main(String args[]) 221 throws Exception 222 { 223 StringBuffer data = new StringBuffer (); 224 FileReader in = new FileReader("post.txt"); 225 int c; 226 while((c=in.read()) != -1) 227 data.append((char)c); 228 PostDataParser p = new PostDataParser(data.toString()); 229 } 231 } 232 | Popular Tags |