81,094
社区成员
发帖
与我相关
我的任务
分享
POST /upload.jsp HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Content-Type: multipart/form-data; boundary=---------------------------7da29f2d890386
Host: 192.9.200.246:8080
Content-Length: 1516663
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------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--
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final int NONE=0;
final int DATAHEADER=1;
final int FILEDATA=2;
final int FIELDDATA=3;
//将请求消息的实体送到b变量中
int TotalBytes=request.getContentLength();
byte[] b=new byte[TotalBytes];
String contentType=request.getContentType();//请求消息类型
String fieldname=""; //表单域的名称
String fieldvalue=""; //表单域的值
String filename=""; //文件名
String boundary=""; //分界符
String lastboundary=""; //结束符
int filesize=0; //文件长度
Hashtable<String, String> formfields=new Hashtable<String, String>();
int pos=contentType.indexOf("boundary=");
//String fileID; //上传的文件ID
if(pos!=-1) //取得分界符和结束符
{
pos+="boundary=".length();
boundary="--"+contentType.substring(pos);
lastboundary=boundary+"--";
}
int state=NONE;
//得到数据输入流reqbuf
DataInputStream in=new DataInputStream(request.getInputStream());
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==lastboundary)||(s==null)) 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);
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);//去掉前面的部分
s=reqbuf.readLine();
b=subBytes(b,s.getBytes().length+4,b.length);
pos=byteIndexOf(b,boundary,0);
b=subBytes(b,0,pos-1);
File f=new File(formfields.get("FilePath")+"\\"+filename); //写入文件
DataOutputStream fileout=new DataOutputStream(new FileOutputStream(f));
fileout.write(b,0,b.length-1);
filesize=b.length-1;
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;
}
}
}
DataInputStream in=new DataInputStream(request.getInputStream());
in.readFully(b);
in.close();
String reqContent=new String(b,"UTF-8");//会造成内存溢出
while ((s = reader.readLine()) != null) {
....(具体处理)
}