|
| 1 | +package org.eh.core.util; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.DataInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.io.StringReader; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.eh.core.model.FileInfo; |
| 12 | + |
| 13 | +/** |
| 14 | + * 解析content获取上传文件表单信息 |
| 15 | + * @author guojing |
| 16 | + * @date 2014-3-10 |
| 17 | + */ |
| 18 | +public class FileUploadContentAnalysis { |
| 19 | + static final int NONE = 0; |
| 20 | + static final int DATAHEADER = 1; |
| 21 | + static final int FILEDATA = 2; |
| 22 | + static final int FIELDDATA = 3; |
| 23 | + static final int MXA_SEGSIZE = 1000 * 1024 * 10; //最大文件长度 |
| 24 | + |
| 25 | + public static Map<String, Object> parse(InputStream ins, String contentType, int totalLength) |
| 26 | + throws IOException { |
| 27 | + |
| 28 | + FileInfo fileInfo =new FileInfo(); |
| 29 | + String fieldname = ""; // 表单域的名称 |
| 30 | + String fieldvalue = ""; // 表单域的值 |
| 31 | + String filename = ""; // 文件名 |
| 32 | + String boundary = ""; // 分界符 |
| 33 | + String lastboundary = ""; // 结束符 |
| 34 | + String filefieldname = ""; // 文件表单域名 |
| 35 | + Map<String, Object> formfields = new HashMap<String, Object>(); |
| 36 | + int filesize = 0; // 文件长度 |
| 37 | + |
| 38 | + int pos = contentType.indexOf("boundary="); |
| 39 | + |
| 40 | + if (pos != -1) { // 取得分界符和结束符 |
| 41 | + pos += "boundary=".length(); |
| 42 | + boundary = "--" + contentType.substring(pos); |
| 43 | + lastboundary = boundary + "--"; |
| 44 | + } |
| 45 | + int state = NONE; |
| 46 | + // 得到数据输入流reqbuf |
| 47 | + DataInputStream in = new DataInputStream(ins); |
| 48 | + // 将请求消息的实体送到b变量中 |
| 49 | + int totalBytes = totalLength; |
| 50 | + String message = ""; |
| 51 | + if (totalBytes > MXA_SEGSIZE) {// 每批大于10m时 |
| 52 | + message = "Each batch of data can not be larger than " + MXA_SEGSIZE / (1000 * 1024) |
| 53 | + + "M"; |
| 54 | + return null; |
| 55 | + } |
| 56 | + byte[] b = new byte[totalBytes]; |
| 57 | + in.readFully(b); |
| 58 | + in.close(); |
| 59 | + String reqContent = new String(b, "UTF-8");// |
| 60 | + BufferedReader reqbuf = new BufferedReader(new StringReader(reqContent)); |
| 61 | + |
| 62 | + boolean flag = true; |
| 63 | + int i = 0; |
| 64 | + while (flag == true) { |
| 65 | + String s = reqbuf.readLine(); |
| 66 | + if ((s == null) || (s.equals(lastboundary))) |
| 67 | + break; |
| 68 | + |
| 69 | + switch (state) { |
| 70 | + case NONE: |
| 71 | + if (s.startsWith(boundary)) { |
| 72 | + state = DATAHEADER; |
| 73 | + i += 1; |
| 74 | + } |
| 75 | + break; |
| 76 | + case DATAHEADER: |
| 77 | + pos = s.indexOf("filename="); |
| 78 | + if (pos == -1) { // 将表单域的名字解析出来 |
| 79 | + pos = s.indexOf("name="); |
| 80 | + pos += "name=".length() + 1; |
| 81 | + s = s.substring(pos); |
| 82 | + int l = s.length(); |
| 83 | + s = s.substring(0, l - 1); |
| 84 | + fieldname = s; |
| 85 | + state = FIELDDATA; |
| 86 | + } else { |
| 87 | + String temp = s; |
| 88 | + // 将文件表单参数名解析出来 |
| 89 | + pos = s.indexOf("name="); |
| 90 | + pos += "name=".length() + 1; |
| 91 | + s = s.substring(pos); |
| 92 | + int pos1 = s.indexOf("\";"); |
| 93 | + filefieldname = s.substring(0, pos1); |
| 94 | + |
| 95 | + // 将文件名解析出来 |
| 96 | + pos = s.indexOf("filename="); |
| 97 | + pos += "filename=".length() + 1; |
| 98 | + s = s.substring(pos); |
| 99 | + int l = s.length(); |
| 100 | + s = s.substring(0, l - 1);// 去掉最后那个引号” |
| 101 | + pos = s.lastIndexOf("\\"); |
| 102 | + s = s.substring(pos + 1); |
| 103 | + filename = s; |
| 104 | + // 从字节数组中取出文件数组 |
| 105 | + pos = byteIndexOf(b, temp, 0); |
| 106 | + b = subBytes(b, pos + temp.getBytes().length + 2, b.length);// 去掉前面的部分 |
| 107 | + int n = 0; |
| 108 | + /** |
| 109 | + * 过滤boundary下形如 Content-Disposition: form-data; name="bin"; filename="12.pdf" Content-Type: |
| 110 | + * application/octet-stream Content-Transfer-Encoding: binary 的字符串 |
| 111 | + */ |
| 112 | + while ((s = reqbuf.readLine()) != null) { |
| 113 | + if (n == 1) |
| 114 | + break; |
| 115 | + if (s.equals("")) |
| 116 | + n++; |
| 117 | + |
| 118 | + b = subBytes(b, s.getBytes().length + 2, b.length); |
| 119 | + } |
| 120 | + pos = byteIndexOf(b, boundary, 0); |
| 121 | + if (pos != -1) |
| 122 | + b = subBytes(b, 0, pos - 1); |
| 123 | + |
| 124 | + filesize = b.length - 1; |
| 125 | + state = FILEDATA; |
| 126 | + } |
| 127 | + break; |
| 128 | + case FIELDDATA: |
| 129 | + s = reqbuf.readLine(); |
| 130 | + fieldvalue = s; |
| 131 | + formfields.put(fieldname, fieldvalue); |
| 132 | + state = NONE; |
| 133 | + break; |
| 134 | + case FILEDATA: |
| 135 | + while ((!s.startsWith(boundary)) && (!s.startsWith(lastboundary))) { |
| 136 | + s = reqbuf.readLine(); |
| 137 | + if (s.startsWith(boundary)) { |
| 138 | + state = DATAHEADER; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + fileInfo.setFieldname(filefieldname); |
| 146 | + fileInfo.setBytes(b); |
| 147 | + fileInfo.setFilename(filename); |
| 148 | + fileInfo.setLength(filesize); |
| 149 | + formfields.put(filefieldname, fileInfo); |
| 150 | + return formfields; |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + // 字节数组中的INDEXOF函数,与STRING类中的INDEXOF类似 |
| 155 | + public static int byteIndexOf(byte[] b, String s, int start) { |
| 156 | + return byteIndexOf(b, s.getBytes(), start); |
| 157 | + } |
| 158 | + |
| 159 | + // 字节数组中的INDEXOF函数,与STRING类中的INDEXOF类似 |
| 160 | + public static int byteIndexOf(byte[] b, byte[] s, int start) { |
| 161 | + int i; |
| 162 | + if (s.length == 0) { |
| 163 | + return 0; |
| 164 | + } |
| 165 | + int max = b.length - s.length; |
| 166 | + if (max < 0) |
| 167 | + return -1; |
| 168 | + if (start > max) |
| 169 | + return -1; |
| 170 | + if (start < 0) |
| 171 | + start = 0; |
| 172 | + search: for (i = start; i <= max; i++) { |
| 173 | + if (b[i] == s[0]) { |
| 174 | + int k = 1; |
| 175 | + while (k < s.length) { |
| 176 | + if (b[k + i] != s[k]) { |
| 177 | + continue search; |
| 178 | + } |
| 179 | + k++; |
| 180 | + } |
| 181 | + return i; |
| 182 | + } |
| 183 | + } |
| 184 | + return -1; |
| 185 | + } |
| 186 | + |
| 187 | + // 用于从一个字节数组中提取一个字节数组 |
| 188 | + public static byte[] subBytes(byte[] b, int from, int end) { |
| 189 | + byte[] result = new byte[end - from]; |
| 190 | + System.arraycopy(b, from, result, 0, end - from); |
| 191 | + return result; |
| 192 | + } |
| 193 | + |
| 194 | + // 用于从一个字节数组中提取一个字符串 |
| 195 | + public static String subBytesString(byte[] b, int from, int end) { |
| 196 | + return new String(subBytes(b, from, end)); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments