1 12 package filters.upload; 13 14 import java.util.StringTokenizer ; 16 import java.util.ArrayList ; 17 import java.util.Hashtable ; 18 import java.util.List ; 19 20 import java.io.FileOutputStream ; 22 import java.io.File ; 23 import java.io.IOException ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.ServletInputStream ; 27 28 32 public class FileUpload { 33 34 private HttpServletRequest req; 36 37 private String dir ; 39 40 private String fileName; 42 43 private Hashtable map = new Hashtable (); 45 46 private String boundary = "--"; 48 49 private byte[] buff= new byte[100*1024]; 50 51 private ServletInputStream in; 53 54 private String paramName; 56 57 private String contentType = null; 58 59 67 public FileUpload(HttpServletRequest r,String uploadDir) throws IOException { 68 req = r; 69 dir = uploadDir; 70 upload(); 71 } 72 73 80 private String readLine() throws IOException { 81 int len = 0; 82 String line = null; 83 len= in.readLine(buff,0,buff.length); 84 if (len<0) return null; 85 line = new String (buff,0,len, "ISO-8859-1"); 86 87 return line; 88 } 89 90 96 public void upload() throws IOException { 97 setBoundary(); 99 100 in = req.getInputStream(); 102 103 String line = readLine(); 105 while((line= readLine())!=null) { 106 setHeaders(line); 108 109 line=readLine(); 111 112 if(line.toLowerCase().startsWith("content-type")){ 115 contentType = line.substring(line.indexOf(":")+1).trim(); 116 line=readLine(); 117 writeToFile(); 118 continue; 119 } else { 120 readParam(); 122 } 123 } 124 } 125 126 129 private void setBoundary()throws IOException { 130 String temp = req.getContentType(); 131 int index = temp.indexOf("boundary="); 132 boundary += temp.substring(index+9,temp.length()); 133 } 134 135 136 139 private void readParam() throws IOException { 140 String line = null; 141 StringBuffer buf= new StringBuffer (); 142 while (!(line=readLine()).startsWith(boundary)){ 143 buf.append(line); 144 } 145 line = buf.substring(0,buf.length()-2); 146 if(map.containsKey(paramName)) { 147 Object existingValue = map.get(paramName); 148 List valueList = null; 149 if( existingValue instanceof List ) { 150 valueList = (List )existingValue; 151 } else { 152 valueList= new ArrayList (); 153 valueList.add(existingValue); 154 } 155 valueList.add(line); 156 map.put(paramName,valueList); 157 } 158 map.put(paramName,line); 159 } 160 161 165 public void setHeaders( String line){ 166 StringTokenizer tokens = new StringTokenizer (line,";",false); 167 String token = tokens.nextToken(); 168 String temp = token.toLowerCase(); 169 int index = temp.indexOf("content-disposition="); 170 171 token = tokens.nextToken(); 172 temp = token.toLowerCase(); 173 index = token.indexOf("name="); 174 paramName = token.substring(index+6,token.lastIndexOf('"')); 175 fileName = null; 176 177 if (tokens.hasMoreTokens()) { 178 token = tokens.nextToken(); 179 temp = token.toLowerCase(); 180 index = token.indexOf("filename="); 181 fileName = token.substring(index+10,token.length()); 182 index = fileName.lastIndexOf('/'); 183 if(index<0) { 184 index = fileName.lastIndexOf('\\'); 185 } 186 if(index <0) { 187 fileName = fileName.substring(0,fileName.lastIndexOf('"')); 188 } else { 189 fileName = fileName.substring(index+1,fileName.lastIndexOf('"')); 190 } 191 } 192 } 193 194 198 private void writeToFile() throws IOException { 199 FileOutputStream out = new FileOutputStream (dir+File.separator+fileName); 201 202 boolean writeCR = false; 207 int len = 0; 208 String line = null; 209 map.put(paramName, dir + File.separator + fileName); 210 map.put(paramName + ".filename", fileName); 211 map.put(paramName + ".content-type", contentType); 212 213 while((len=in.readLine(buff,0,buff.length))>-1) { 215 line = new String (buff,0,len); 216 217 if (line.startsWith(boundary)) break; 219 if(writeCR){ 220 writeCR=false; 221 out.write('\r'); 222 out.write('\n'); 223 } 224 if(len>2 && buff[len-2]=='\r' && buff[len-1]=='\n') { 225 writeCR=true; 226 out.write(buff,0,len-2); 227 } else { 228 out.write(buff,0,len); 229 } 230 } 231 out.close(); 232 } 233 234 240 public String getParameter( String name ) { 241 Object val = map.get(name); 242 if(val instanceof String ) { 243 return (String )val; 244 } else { 245 List vals = (List )val; 246 return (String )vals.get(0); 247 } 248 } 249 250 258 public java.util.Enumeration getParameterNames() { 259 return map.keys(); 260 } 261 262 270 public String [] getParameterValues( String name ) { 271 272 if (!map.containsKey(name)) 273 return null; 274 275 Object val = map.get(name); 276 if(val instanceof String ){ 277 return new String []{(String )val}; 278 } else { 279 List vals = (List )val; 280 return (String [])vals.toArray(new String [vals.size()]); 281 } 282 } 283 } 284 | Popular Tags |