1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| package org.xmh.demo;
import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.Hashtable;
public class FileUploadBean { private String savePath; private String filepath; private String filename; private String contentType; private Hashtable<String, String> fields; public String getFilename() { return filename; } public String getFilepath() { return filepath; } public void setSavePath(String savePath) { this.savePath = savePath; } public String getContentType() { return contentType; } public String getFieldValue(String fieldName) { if (fields == null || fieldName == null) return null; return (String) fields.get(fieldName); } private void setFilename(String s) { if (s == null) return; int pos = s.indexOf("filename=\""); if (pos != -1) { filepath = s.substring(pos + 10, s.length() - 1);
pos = filepath.lastIndexOf("\\"); if (pos != -1) filename = filepath.substring(pos + 1); else filename = filepath; } } private void setContentType(String s) { if (s == null) return; int pos = s.indexOf(": "); if (pos != -1) contentType = s.substring(pos + 2, s.length()); } public void doUpload(HttpServletRequest request) throws IOException { ServletInputStream in = request.getInputStream(); byte[] line = new byte[128]; int i = in.readLine(line, 0, 128); if (i < 3) return; int boundaryLength = i - 2; String boundary = new String(line, 0, boundaryLength); fields = new Hashtable<String, String>(); while(i!=-1){ String newLine=new String(line,0,i);
if(newLine.startsWith("Content-Disposition: form-data; name=\"")){ if(newLine.indexOf("filename=\"") != -1){ setFilename(new String(line,0,i-2)); if(filename == null){ return; } i=in.readLine(line,0,128); setContentType(new String(line,0,i-2)); i=in.readLine(line,0,128); newLine=new String(line,0,i); PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter((savePath == null ? "":savePath)+filename))); while(i!=-1 && !newLine.startsWith(boundary)){ i=in.readLine(line,0,128); if( (i == boundaryLength+2 || i==boundaryLength+4) && (new String(line,0,i).startsWith(boundary))){ pw.print(newLine.substring(0,newLine.length()-2)); }else{ pw.print(newLine); } newLine=new String(line,0,i); } pw.close(); }else{ int pos=newLine.indexOf("name=\""); String fieldName=newLine.substring(pos+6,newLine.length()-3); i=in.readLine(line,0,128); newLine=new String(line,0,i); StringBuffer fieldValue=new StringBuffer(128); while(i!=-1 && !newLine.startsWith(boundary)){ i=in.readLine(line,0,128); if( (i==boundaryLength+2 || i==boundaryLength+4) &&(new String(line,0,i).startsWith(boundary))){ fieldValue.append(newLine.substring(0,newLine.length()-2)); }else fieldValue.append(newLine);
newLine=new String(line,0,i); } fields.put(fieldName,fieldValue.toString()); }
} i=in.readLine(line,0,128); } } }
|