本人觉的文件上传无分就是分解字符串的东西
以下是文件上传的一个类
package UploadFile;
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 ServletException,IOException{
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;                //Îļþ³¤¶È
//String strContentName="";       //ÿһ´Î¶ÁÈ¡Êý¾ÝÏîʱ¼ÇסÕâÒ»×Ö·û
//ÈÝÄÉ±íµ¥ÓòµÄÃû³Æ/ÖµµÄ¹þÏ£±í
Hashtable formfields=new Hashtable();
//ÔÚÏûϢͷÀàÐÍÖÐÕÒµ½·Ö½ç·ûµÄ¶¨Òå
int pos=contentType.indexOf("boundary=");
String fileID;                  //ÉÏ´«µÄÎļþID
if(pos!=-1){
pos+="boundary=".length();
boundary="--" + contentType.substring(pos);  //½âÎö³ö·Ö½ç·û
lastboundary=boundary+"--";                  //µÃµ½½áÊø·Ö½ç·û
}
int state=NONE;                //Æðʼ״̬ΪNONE
//µÃµ½ÇëÇóÏûÏ¢µÄÊý¾ÝÊäÈëÁ÷
DataInputStream in=new DataInputStream(req.getInputStream());
in.readFully(b);               //¸ù¾Ý³¤¶È,½«ÏûϢʵÌåµÄÄÚÈݶÁÈëÊý×ébÖÐ
in.close();                    //¹Ø±ÕÊý¾ÝÁ÷
String reqcontent=new String(b); //´Ó×Ö½ÚÊý×éÖеõ½±íʾʵÌåµÄ×Ö·û´®
//´Ó×Ö·û´®Öеõ½Êä³ö»º³åÁ÷
BufferedReader reqbuf=new BufferedReader(new StringReader(reqcontent));

解决方案 »

  1.   

    //ÉèÖÃÑ­»·±êÖ¾
    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=");
             // strContentName=s;
              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;
                 if (filename.equals("")){state=FILEDATA;break;}
              pos=byteIndexOf(b,temp,0);
             // b=subBytes(b,pos+temp.getBytes().lenght+2,b.lenght); 
                 b=subBytes(b,pos+temp.getBytes().length+2,b.length);
              s=reqbuf.readLine();
             
              File f=new File(getServletContext().getRealPath("/") + "upload/" + 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);
              fileout.close();
              fileSize=b.length-1;
              state=FILEDATA;
             }
             break;
         case FIELDDATA:
             
             //byte[] bytemp=b;
             //pos=byteIndexOf(bytemp,strContentName,0);
             //bytemp=subBytes(bytemp,pos+strContentName.getBytes().length+4,bytemp.length);
             //pos=byteIndexOf(bytemp,boundary,0);
             //bytemp=subBytes(bytemp,0,pos-1);
             //fieldvalue=bytemp.toString();
             s=reqbuf.readLine();
             fieldvalue=s;
             while((!s.startsWith(boundary)) && (!s.startsWith(lastboundary))){
                s=reqbuf.readLine();
                if (s.startsWith(boundary)) break;
                fieldvalue=fieldvalue + "\r\n" + s;
                
             }
             formfields.put(fieldname,fieldvalue);
             state=DATAHEADER;
             break;
         case FILEDATA:
         while((!s.startsWith(boundary)) && (!s.startsWith(lastboundary))){
         s=reqbuf.readLine();
         if (s.startsWith(boundary)){
         state=DATAHEADER;
         break;
         }
         //else break;
         }
         break;
         }
    }

    res.setContentType("text/html;charset=gb2312");
    PrintWriter out=res.getWriter();
    out.println("<html><head><title>&Icirc;&Auml;&frac14;&thorn;&Eacute;&Iuml;&acute;&laquo;&frac12;á&sup1;&ucirc;</title></head><body><h1>&Icirc;&Auml;&frac14;&thorn;&Eacute;&Iuml;&acute;&laquo;&frac12;á&sup1;&ucirc;</h1><br>");
    out.println("ID&Icirc;&ordf;" + formfields.get("FileID")+"&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;"+filename+"&Ograve;&Ntilde;&frac34;&shy;&Eacute;&Iuml;&acute;&laquo;!"+"&Icirc;&Auml;&frac14;&thorn;&sup3;¤&para;&Egrave;&Icirc;&ordf;:"+fileSize+"×&Ouml;&frac12;&Uacute;");
    out.println("<p>Message&Icirc;&ordf;" + formfields.get("Message")+"</p>");
    out.println("<p>&Eacute;&Iuml;&acute;&laquo;&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;&micro;&Oslash;&Ouml;·:<a href=\"/Forums/upload/" + filename +"\">&sup2;é&iquest;&acute;</a></p>");
    // out.println("<p>Order&Icirc;&ordf;" + formfields.get("Order")+"&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;</p>");
    out.println("</body></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));
    }
       
    }