62,614
社区成员
发帖
与我相关
我的任务
分享
POST /upload.jsp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7da29f2d890386
Host: abc.com
Content-Length: 1516663
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------7da29f2d890386
Content-Disposition: form-data; name="ServerPath"
localhost
-----------------------------7da29f2d890386
Content-Disposition: form-data; name="id"
12323123
-----------------------------7da29f2d890386
Content-Disposition: form-data; name="FileData"; filename="D:\set.pdf"
Content-Type: application/pdf
%PDF-1.6
%����
1 0 obj<</Type/Page/Contents 2 0 R/Parent 3 0 R/Resources 4 0 R/CropBox[0 0 595.22 842]/MediaBox[0 0 595.22 842]/Rotate 0>>
endobj
4 0 obj<</ColorSpace<</Cs6 5 0 R>>/ExtGState<</GS1 6 0 R>>/ProcSet[/PDF/Text]/Font<</F1 7 0 R/F3 8 0 R/TT3 9 0 R/F2 10 0 R>>>>
endobj
2 0 obj <</Filter/FlateDecode/Length 1887>>stream
-----------------------------7da29f2d890386--
private byte[] parse(HttpServletRequest request) throws IOException {
final int NONE = 0;
final int DATAHEADER = 1;
final int FILEDATA = 2;
final int FIELDDATA = 3;
final int MXA_SEGSIZE = 1000 * 1024 * 10;//每批最大的数据量 10M
String contentType = request.getContentType();// 请求消息类型
String fieldname = ""; // 表单域的名称
String fieldvalue = ""; // 表单域的值
String filename = ""; // 文件名
String boundary = ""; // 分界符
String lastboundary = ""; // 结束符
String filePath = "";
Hashtable<String, String> formfields = new Hashtable<String, String>();
int filesize = 0; // 文件长度
int pos = contentType.indexOf("boundary=");
if (pos != -1) { // 取得分界符和结束符
pos += "boundary=".length();
boundary = "--" + contentType.substring(pos);
lastboundary = boundary + "--";
}
int state = NONE;
// 得到数据输入流reqbuf
DataInputStream in = new DataInputStream(request.getInputStream());
// 将请求消息的实体送到b变量中
int totalBytes = request.getContentLength();
String message = "";
if (totalBytes > MXA_SEGSIZE) {//每批大于10m时
message = "Each batch of data can not be larger than " + MXA_SEGSIZE / (1000 * 1024)
+ "M";
return null;
}
byte[] b = new byte[totalBytes];
in.readFully(b);
in.close();
String reqContent = new String(b, "UTF-8");//
BufferedReader reqbuf = new BufferedReader(new StringReader(reqContent));
boolean flag = true;
int i = 0;
while (flag == true) {
String s = reqbuf.readLine();
if ((s == null) || (s.equals(lastboundary)))
break;
switch (state) {
case NONE:
if (s.startsWith(boundary)) {
state = DATAHEADER;
i += 1;
}
break;
case DATAHEADER:
pos = s.indexOf("filename=");
if (pos == -1) { // 将表单域的名字解析出来
pos = s.indexOf("name=");
pos += "name=".length() + 1;
s = s.substring(pos);
int l = s.length();
s = s.substring(0, l - 1);
fieldname = s;
state = FIELDDATA;
} else { // 将文件名解析出来
String temp = s;
pos = s.indexOf("filename=");
pos += "filename=".length() + 1;
s = s.substring(pos);
int l = s.length();
s = s.substring(0, l - 1);// 去掉最后那个引号”
filePath = s;
pos = s.lastIndexOf("\\");
s = s.substring(pos + 1);
filename = s;
// 从字节数组中取出文件数组
pos = byteIndexOf(b, temp, 0);
b = subBytes(b, pos + temp.getBytes().length + 2, b.length);// 去掉前面的部分
int n = 0;
/**
* 过滤boundary下形如 Content-Disposition: form-data; name="bin";
* filename="12.pdf" Content-Type: application/octet-stream
* Content-Transfer-Encoding: binary 的字符串
*/
while ((s = reqbuf.readLine()) != null) {
if (n == 1)
break;
if (s.equals(""))
n++;
b = subBytes(b, s.getBytes().length + 2, b.length);
}
pos = byteIndexOf(b, boundary, 0);
if (pos != -1)
b = subBytes(b, 0, pos - 1);
filesize = b.length - 1;
formfields.put("filesize", String.valueOf(filesize));
state = FILEDATA;
}
break;
case FIELDDATA:
s = reqbuf.readLine();
fieldvalue = s;
formfields.put(fieldname, fieldvalue);
state = NONE;
break;
case FILEDATA:
while ((!s.startsWith(boundary)) && (!s.startsWith(lastboundary))) {
s = reqbuf.readLine();
if (s.startsWith(boundary)) {
state = DATAHEADER;
break;
}
}
break;
}
}
return b;
}
// 字节数组中的INDEXOF函数,与STRING类中的INDEXOF类似
public static int byteIndexOf(byte[] b, String s, int start) {
return byteIndexOf(b, s.getBytes(), start);
}
// 字节数组中的INDEXOF函数,与STRING类中的INDEXOF类似
public static int byteIndexOf(byte[] b, byte[] s, int start) {
int i;
if (s.length == 0) {
return 0;
}
int max = b.length - s.length;
if (max < 0)
return -1;
if (start > max)
return -1;
if (start < 0)
start = 0;
search: for (i = start; i <= max; i++) {
if (b[i] == s[0]) {
int k = 1;
while (k < s.length) {
if (b[k + i] != s[k]) {
continue search;
}
k++;
}
return i;
}
}
return -1;
}
// 用于从一个字节数组中提取一个字节数组
public static byte[] subBytes(byte[] b, int from, int end) {
byte[] result = new byte[end - from];
System.arraycopy(b, from, result, 0, end - from);
return result;
}
// 用于从一个字节数组中提取一个字符串
public static String subBytesString(byte[] b, int from, int end) {
return new String(subBytes(b, from, end));
}
主要实现是parse,返回的是这段的字节数组。比较繁琐