1 24 25 package org.ofbiz.base.util; 26 27 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.util.Date ; 32 import java.util.Dictionary ; 33 import java.util.Hashtable ; 34 35 import javax.servlet.ServletInputStream ; 36 import javax.servlet.http.HttpServletRequest ; 37 38 39 45 public class HttpRequestFileUpload { 46 47 private int BUFFER_SIZE = 4096; 48 private int WAIT_INTERVAL = 200; private int MAX_WAITS = 20; 50 private int waitCount = 0; 51 private String savePath; 52 private String filepath; 53 private String filename; 54 private String contentType; 55 private String overrideFilename = null; 56 private Dictionary fields; 57 58 public String getOverrideFilename() { 59 return overrideFilename; 60 } 61 62 public void setOverrideFilename(String ofName) { 63 overrideFilename = ofName; 64 } 65 66 public String getFilename() { 67 return filename; 68 } 69 70 public String getFilepath() { 71 return filepath; 72 } 73 74 public void setSavePath(String savePath) { 75 this.savePath = savePath; 76 } 77 78 public String getContentType() { 79 return contentType; 80 } 81 82 public String getFieldValue(String fieldName) { 83 if (fields == null || fieldName == null) 84 return null; 85 return (String ) fields.get(fieldName); 86 } 87 88 private void setFilename(String s) { 89 if (s == null) 90 return; 91 92 int pos = s.indexOf("filename=\""); 93 94 if (pos != -1) { 95 filepath = s.substring(pos + 10, s.length() - 1); 96 pos = filepath.lastIndexOf("\\"); 100 if (pos != -1) 101 filename = filepath.substring(pos + 1); 102 else 103 filename = filepath; 104 } 105 } 106 107 private void setContentType(String s) { 108 if (s == null) 109 return; 110 111 int pos = s.indexOf(": "); 112 113 if (pos != -1) 114 contentType = s.substring(pos + 2, s.length()); 115 } 116 117 public void doUpload(HttpServletRequest request) throws IOException { 118 ServletInputStream in = request.getInputStream(); 119 120 126 String reqLengthString = request.getHeader("content-length"); 127 128 System.out.println("expect " + reqLengthString + " bytes."); 129 int requestLength = 0; 130 131 try { 132 requestLength = new Integer (reqLengthString).intValue(); 133 } catch (Exception e2) { 134 e2.printStackTrace(); 135 return; 136 } 137 byte[] line = new byte[BUFFER_SIZE]; 138 139 int i = -1; 140 141 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 142 requestLength -= i; 143 if (i < 3) 144 return; 145 int boundaryLength = i - 2; 146 147 String boundary = new String (line, 0, boundaryLength); 149 System.out.println("boundary=[" + boundary + "] length is " + boundaryLength); 150 fields = new Hashtable (); 151 152 while (requestLength > 0) { 153 String newLine = ""; 154 155 if (i > -1) { 156 newLine = new String (line, 0, i); 157 } 158 if (newLine.startsWith("Content-Disposition: form-data; name=\"")) { 159 if (newLine.indexOf("filename=\"") != -1) { 160 setFilename(new String (line, 0, i - 2)); 161 if (filename == null) 162 return; 163 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 165 requestLength -= i; 166 167 setContentType(new String (line, 0, i - 2)); 168 169 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 171 requestLength -= i; 172 newLine = new String (line, 0, i); 173 String filenameToUse = filename; 174 175 if (overrideFilename != null) { 176 filenameToUse = overrideFilename; 177 } 178 179 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 181 requestLength -= i; 182 newLine = new String (line, 0, i); 183 184 byte[] lastTwoBytes = new byte[2]; 185 186 if (i > 1) { 187 lastTwoBytes[0] = line[i - 2]; 188 lastTwoBytes[1] = line[i - 1]; 189 } 190 System.out.println("about to create a file:" + (savePath == null ? "" : savePath) + filenameToUse); 191 File savePathFile = new File (savePath); 193 if (!savePathFile.exists()) { 194 savePathFile.mkdirs(); 195 } 196 FileOutputStream fos = new FileOutputStream ((savePath == null ? "" : savePath) + filenameToUse); 197 boolean bail = (new String (line, 0, i).startsWith(boundary)); 198 boolean oneByteLine = (i == 1); 200 while ((requestLength > 0) && !bail) { 201 202 if (i > 1) { 204 fos.write(line, 0, i - 2); 205 } 206 207 oneByteLine = (i == 1); 209 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 210 requestLength -= i; 211 212 215 219 if (requestLength < 1) { 220 bail = true; 221 } else if (oneByteLine) { 222 fos.write(lastTwoBytes, 0, 1); } else { 224 fos.write(lastTwoBytes, 0, 2); 225 } 226 227 if (i > 1) { 228 lastTwoBytes[0] = line[i - 2]; 230 lastTwoBytes[1] = line[i - 1]; 231 } else { 232 lastTwoBytes[0] = line[0]; } 234 } 235 fos.flush(); 236 fos.close(); 237 } else { 238 int pos = newLine.indexOf("name=\""); 241 String fieldName = newLine.substring(pos + 6, newLine.length() - 3); 242 243 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 246 requestLength -= i; 247 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 248 requestLength -= i; 249 newLine = new String (line, 0, i); 250 StringBuffer fieldValue = new StringBuffer (BUFFER_SIZE); 251 252 while (requestLength > 0 && !newLine.startsWith(boundary)) { 253 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 258 requestLength -= i; 259 if ((i == boundaryLength + 2 || i == boundaryLength + 4) && (new String (line, 0, i).startsWith(boundary))) 261 fieldValue.append(newLine.substring(0, newLine.length() - 2)); 262 else 263 fieldValue.append(newLine); 264 newLine = new String (line, 0, i); 265 } 266 fields.put(fieldName, fieldValue.toString()); 268 } 269 } 270 i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength); 271 if (i > -1) { 272 requestLength -= i; 273 } 274 275 } } 277 278 private int waitingReadLine(ServletInputStream in, byte[] buf, int off, int len, int reqLen) throws IOException { 280 int i = -1; 281 282 while (((i = in.readLine(buf, off, len)) == -1) && (reqLen > 0)) { 283 System.out.print("waiting"); 284 if (waitCount > MAX_WAITS) { 285 System.out.println("waited " + waitCount + " times, bailing out while still expecting " + 286 reqLen + " bytes."); 287 throw new IOException ("waited " + waitCount + " times, bailing out while still expecting " + 288 reqLen + " bytes."); 289 } 290 waitCount++; 291 long endMS = new Date ().getTime() + WAIT_INTERVAL; 292 293 while (endMS > (new Date ().getTime())) { 294 try { 295 wait(WAIT_INTERVAL); 296 } catch (Exception e3) { 297 System.out.print("."); 298 } 299 } 300 System.out.println((new Date ().getTime() - endMS) + " ms"); 301 } 302 return i; 303 } 304 } 305 | Popular Tags |