一个例子!!import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class UploadFileServlet extends HttpServlet
{
    public void doPost(HttpServletRequest req,
                      HttpServletResponse res)
        throws IOException, ServletException{
        final int NONE=0;
        final int DATAHEADER=1;
        final int FILEDATA=2;
        final int FIELDDATA=3;
        int totalbytes=req.getContentLength();
        byte[] b=new byte[totalbytes];
        String contentType=req.getContentType();
        String fieldname="";
        String fieldvalue="";
        String filename="";
        String boundary="";
        String lastboundary="";
        int fileSize=0;
        Hashtable formfields=new Hashtable();
        int pos =contentType.indexOf("boundary=");
        String fileID;
        if(pos!=-1){
            pos+="boundary=".length();
            boundary="--"+contentType.substring(pos);
            lastboundary=boundary+"--";
        }
        int state=NONE;
        DataInputStream in=new DataInputStream(req.getInputStream());
        in.readFully(b);
        in.close();
        String reqcontent=new String(b);
        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;
                    }
                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();
                        File f=new File(filename);
                        DataOutputStream fileout=new DataOutputStream(new FileOutputStream(f));
                        b=subBytes(b,s.getBytes().length+4,b.length);
                        pos=byteIndexOf(b,boundary,0);
                        b=subBytes(b,0,pos-1);
                        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;
                        }
                        else break;
                        break;
                    }
                }
            }
            res.setContentType("text/html;charset=gb2312");
            PrintWriter out=res.getWriter();
            out.println("<html>");
            out.println("<head><title>文件上传结果</title></head>");
            out.println("<body>");
            out.println("<h1>文件上传结果</h1><hr>");
            out.println("ID为"+formfields.get("FileID")+"的文件"+filename+"已经上传!"+"文件长度:"+fileSize+"字节");
            out.println("</body>");
            out.println("</html>");
    }
    private static int byteIndexOf(byte[] b,String s,int start)
    {
        return byteIndexOf(b,s.getBytes(),start);
    }
    private 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;
    }
    private 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;
    }
    private static String subBytesString(byte[] b,int from, int end)
    {
        return new String(subBytes(b,from,end));
    }
}