做个参考吧import java.io.*;
import java.util.*;
import javax.servlet.http.*;
/**
 * 多媒体文档解析器
 */public class MIMEParser
    extends TokenParser {
  /**
   * 表单上的控件名称和内容
   */
  private Hashtable controls = new Hashtable();
  private Hashtable filenameAndAddress = new Hashtable();  /**
   * 构造方法
   * @param request
   * @throws Exception
   */
  public MIMEParser(HttpServletRequest request) throws Exception {
    super(request.getInputStream());
    parse();
  }  /**
   * 得到一行数据
   */
  public byte[] getLine() throws Exception {
    byte[] result = new byte[1024];
    byte t = this.getChar();
    int count = 0;
    //行没有结束时
    //循环读字节
    while ( ( ( (char) t) != '\n') && ( (char) t) != '\r' && count < 1023) {
      result[count] = t;
      //读下一个字符
      t = this.getChar();
      count++;    }
    result[count] = t;
    count++;
    byte[] temp = new byte[count];
    System.arraycopy(result, 0, temp, 0, temp.length);
    return temp;  }  /**控件名称
   * 得到上传的一个文件的内容
   * @param name
   * @return
   */
  public byte[] getFileContent(String name) {
    return null;
  }  /**
   * 解析MIME协议,数据流格式为:
   * --------------------78913
   * file type: context name="abc"
   *
   * abcd
   * --------------------78913
   * file type: context name="file" filename="abc"
   *
   * fds
   * fds
   * --------------------78913
   * @param inData 解析数据
   * @return
   */
  public void parse() throws Exception {
    //读出分界符
    try {
      byte[] line = getLine();
      //因为读入的每 一行,有'\n\\r\ 等,所以为了判断,去掉这个char
      byte[] pd = new byte[line.length - 1];
      System.arraycopy(line, 0, pd, 0, line.length - 1);
      while (line != null) {
        // 得到内容描述那一行
        String con = new String(getLine());
        while (con.indexOf("Content-Disposition:") == -1) {
          con = new String(getLine());
        }
        /**
         * 如果得到则开始找到控件的名称和有没有文件
         */        if (con != null && !con.equals("")) {
          //找到控件的名称的位置
          int posion = con.indexOf("name=\"");
          //找到文件的位置
          int filename = con.indexOf("filename=\"");
          //如果没有文件名称则说明,这是个控件,而不是一个文件
          if (posion != 0 && filename == -1) {
            //得到控件名的终点
            int endposion = con.indexOf("\"", posion + 6);
            //得到控件名的名称
            con = con.substring(posion + 6, endposion);
            //得到控件的内容 
            byte[] conext = this.getLine();
            String cont = "";
            //如果不是分界行,则一直得到内容 
            while (new String(conext).indexOf(new String(pd)) == -1) {
              cont += new String(conext);
              conext = this.getLine();
            }
            //把控件的名称和内容 放到一个哈希表中
            String id = Integer.toString(SequenceFind.getID());
            controls.put(id + "_" + con, cont);          }
          //如果有文2件名,则说明这是一个文件
          else if (posion != 0 && filename != -1) {
            String extendsname = "";
            //得到文件名的终点
            int endfilename = con.indexOf("\"", filename + 10);
            //取得文件名和完全路径和名字
            con = con.substring(filename, endfilename);
            //为了只得到文 件的名字
            con = con.substring(con.
                                lastIndexOf("\\") + 1);
            extendsname = con.substring(con.lastIndexOf("."));
            //取得文件内容的类型
            byte[] type = this.getLine();
            while (new String(type).indexOf("Content-Type:") == -1) {
              type = this.getLine();
            }
            //读掉多余的行
            this.getLine();
            this.getLine();
            this.getLine();
            //保存主件时,用一个唯一的ID号,在前面,为了让别人上传相同的文件
            String address = Integer.toString(SequenceFind.getID());
            String path = PropertiesService.getProperty("attackment", "path");
            FileOutputStream out = new FileOutputStream(path + address + "_" +
                extendsname);
            //取是文 件的内容 ,一次只取一行,且保存一次
            byte[] context = this.getLine();
            while (new String(context).indexOf(new String(pd)) == -1) {
              out.write(context);
              context = this.getLine();
            }
            //把文 件的名字,路径存入哈希表中
            filenameAndAddress.put(address + "_" + con,
                                   path + address + "_" + con);          }
        }
      }
    }
    catch (StreamEndException ee) {}
  }  /**
   * 得到控件名称
   * @param lineParser
   * @return
   * @throws Exception
   */
  public Hashtable getControlNameAndContent() throws Exception {
    return this.controls;  }  public Hashtable getFileAndAddress() throws Exception {
    return this.filenameAndAddress;
  }}