http://blog.csdn.net/gjd111686/archive/2004/08/18/78324.aspx

解决方案 »

  1.   

    用JSP分析multipart/form-data基于表单的文件上传 
    <%
     int iTotalByte,iTotalRead,iReadByte;
     iTotalByte=request.getContentLength(); 
     iTotalRead=0;
     iReadByte=0;
     byte[] Buffer=new byte[iTotalByte];
     if(iTotalByte>0)
     {
      for(;iTotalRead<iTotalByte;iTotalRead+=iReadByte)
      {
       try
       {
    iReadByte=request.getInputStream().read(Buffer,iTotalRead,iTotalByte-iTotalRead);
       }
       catch(Exception e)
       {
        e.printStackTrace();
       }
      }
      String strContentType=request.getContentType();
      //数据处理开始
      String strBuffer=new String(Buffer);
      %><!--<br>表单数据:<br>strBuffer<br>--><%
      String strBoundary="--"+strContentType.substring(strContentType.lastIndexOf("=")+1,strContentType.length());
      String strArray[]=strBuffer.split(strBoundary);  String strSubString;
      int iBegin,iEnd;
      iBegin=0;iEnd=0;
      String strFieldName="";
      String strFieldValue="";
      String strFilePath="";
      String strFileName="";
      String strFileType="";
      boolean bTrue;
      bTrue=false;
      int iLocation=0;
      for(int iIndex=1;iIndex<strArray.length-1;iIndex++)
      {
       strSubString=strArray[iIndex];
       iBegin=strSubString.indexOf("name=\"",0);
       if(iBegin!=-1)
       {
        strFieldName="";strFieldValue="";
        strFilePath="";strFileName="";strFileType="";
        iEnd=strSubString.indexOf("\"",iBegin+6);
        strFieldName=strSubString.substring(iBegin+6,iEnd);
        iBegin=strSubString.indexOf("filename=\"",0);        if(iBegin!=-1)
        {
         bTrue=true;
        }
        iEnd=strSubString.indexOf("\r\n\r\n",0);
        if(bTrue==true)
        {
         //文件路径
         strFilePath=strSubString.substring(iBegin+10,strSubString.indexOf("\"",iBegin+10));strFileName=strFilePath.substring(strFilePath.lastIndexOf("\\")+1);
         strFileType=strSubString.substring(strSubString.indexOf("Content-Type: ")+14,strSubString.indexOf("\r\n\r\n"));
         %><!--<br>文件类型:<br>strFileType<br>--><%
         //文件数据
         iBegin=strSubString.indexOf("\r\n\r\n",iBegin);
         strFieldValue=strSubString.substring(iBegin+4);
         strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
         %><!--<br>文件路径:<br>strFilePath<br>文件名称:<br>strFileName<br>--><%
         byte[] pFile=strFieldValue.getBytes();
         byte[] pFileExtend=new byte[pFile.length];
         iLocation=strBuffer.indexOf("filename=\"",iLocation);
         for(int kIndex=iLocation;kIndex<iTotalByte-2;kIndex++)
         {
          if(Buffer[kIndex]==13&&Buffer[kIndex+2]==13)
          {iLocation=kIndex+4;break;}
         }
         for(int nIndex=0;nIndex<pFile.length;nIndex++)
         {
          pFileExtend[nIndex]=Buffer[iLocation+nIndex];
         }
    /*
    //保存到Local Disk;
    FileOutputStream pFileOutputStream=new FileOutputStream("F:\\Site_App\\UploadFile\\"+strFileName);
    pFileOutputStream.write(pFileExtend);
    pFileOutputStream.close();
    */
         session.putValue(strFieldName+"_FileType",strFileType);
         session.putValue(strFieldName+"_FilePath",strFilePath);
         session.putValue(strFieldName+"_FileName",strFileName);
         session.putValue(strFieldName,pFileExtend);
        }
        else
        {
         strFieldValue=strSubString.substring(iEnd+4);
         strFieldValue=strFieldValue.substring(0,strFieldValue.lastIndexOf("\n")-1);
    session.putValue(strFieldName,strFieldValue);
        }
        bTrue=false;
       }
       %><!--<br>表单域名:<br>strFieldName<br>表单域值:<br>strFieldValue<br>--><%
      }
      //数据处理结束
     }
    %>这样(String)session.getValue("表单域名")返回表单域值,而(byte[])session.getValue("File上传控件域名")返回的字节数组就可以用new ByteArrayInputStream(byte[])调用updateBinaryStream来更新到数据库了
      

  2.   

    显示图片注意传递参数为ID号和媒体的类型.
    Type="image/*"
    <%@ page contentType="text/html; charset=gb2312"%>
    <%@ include file="/DataIni/DataOpen.jsp"%>
    <%@ include file="/ScriptLib/Init.jsp"%>
    <%
    String Id,TypeValue;
    Id=request.getParameter("Id");
    TypeValue=request.getParameter("Type");
    odbcQuery="select * from MediaLib where ID="+Id;
    odbcRs=odbcStmt.executeQuery(odbcQuery); byte[] Buffer=new byte[1024*10];
    InputStream InData=null;
    OutputStream outData=null;
    int iSize;
    if(odbcRs.next())
    {
    outData=response.getOutputStream();
    InData=odbcRs.getBinaryStream("MediaFile");
    response.setContentType(TypeValue);
    while(true)
    {
    iSize=InData.read(Buffer);
    if(iSize==-1)
    {
    break;
    }
    outData.write(Buffer,0,iSize);
    }
    outData.flush();
    response.flushBuffer();
    }
    odbcRs.close();
    %>
    <%@ include file="/DataIni/DataClose.jsp"%>
      

  3.   

    摘自:http://www.mifun.net/pengjie/myweb/article/article.asp?id=203&sort=jsp
    用java实现上传下载功能    看看对你有什么用没有
    import  java.io.*;  
    import  javax.servlet.http.HttpServletRequest;  
    import  javax.servlet.ServletInputStream;  
    import  javax.servlet.ServletException;  public  class  upload{
        private  static  String  newline  =  "\n";
        private  String  uploadDirectory  =  ".";
        private  String  ContentType  =  "";
        private  String  CharacterEncoding  =  "";
        private  String  strFileName  =  "";
        private  long  iFileSize  =  0;
              
        private  String  getFileName(String  s){
              int  i  =  s.lastIndexOf("\\");
              if(i  <  0  ||  i  >=  s.length()  -  1){
                    i  =  s.lastIndexOf("/");
                    if(i  <  0  ||  i  >=  s.length()  -  1)
                          return  s;
              }
              strFileName  =  s.substring(i  +  1);
              return  s.substring(i  +  1);
        }
        
        public  String  getUploadFileName(){
              return  strFileName;
        }
        
        public  long  getFileSize(){
              return  iFileSize;
        }
        
        public  void  setUploadDirectory(String  s){
              uploadDirectory  =  s;
        }    public  void  setContentType(String  s){
              ContentType  =  s;
              int  j;
              if((j  =  ContentType.indexOf("boundary="))  !=  -1){
                    ContentType  =  ContentType.substring(j  +  9);
                    ContentType  =  "--"  +  ContentType;
              }
        }
        
        public  void  setCharacterEncoding(String  s){
              CharacterEncoding  =  s;
        }    public  void  uploadFile(  HttpServletRequest  req)  throws  ServletException,  IOException{
              setCharacterEncoding(req.getCharacterEncoding());
              setContentType(req.getContentType());
              uploadFile(req.getInputStream());
        }      public  void  uploadFile(  ServletInputStream  servletinputstream)  throws  ServletException,  IOException{
              String  s5  =  null;
              String  filename  =  null;
              byte  Linebyte[]  =  new  byte[4096];
              byte  outLinebyte[]  =  new  byte[4096];
              int  ai[]  =  new  int[1];
              int  ai1[]  =  new  int[1];
              String  line;
              //得到文件名
              while((line  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding))  !=  null){
                    int  i  =  line.indexOf("filename=");
                    if(i  >=  0){
                          line  =  line.substring(i  +  10);
                          if((i  =  line.indexOf("\""))  >  0)
                                line  =  line.substring(0,  i);
                          break;
                    }
              }
              
              filename  =  line;
              
              if(filename  !=  null  &&  !filename.equals("\"")){
                    filename  =  getFileName(filename);
                    
                    String  sContentType  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding);
                    if(sContentType.indexOf("Content-Type")  >=  0)
                          readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding);
                          
                    //File(String  parent,  String  child)
                    //Creates  a  new  File  instance  from  a  parent  pathname  string
                    //and  a  child  pathname  string.
                    
                    File  file  =  new  File(uploadDirectory,  filename);
                    
                    //FileOutputStream(File  file)
                    //Creates  a  file  output  stream  to  write  to  the  file  represented
                    //by  the  specified  File  object.
                    
                    FileOutputStream  fileoutputstream  =  new  FileOutputStream(file);
                    
                    while((sContentType  =  readLine(Linebyte,  ai,  servletinputstream,  CharacterEncoding))  !=  null){
                          if(sContentType.indexOf(ContentType)  ==  0  &&  Linebyte[0]  ==  45)
                                break;
                          if(s5  !=  null){
                          
                                //write(byte[]  b,  int  off,  int  len)
                                //Writes  len  bytes  from  the  specified  byte  array  starting
                                //at  offset  off  to  this  file  output  stream.
                          
                                fileoutputstream.write(outLinebyte,  0,  ai1[0]);
                                fileoutputstream.flush();
                          }
                          s5  =  readLine(outLinebyte,  ai1,  servletinputstream,  CharacterEncoding);
                          if(s5  ==  null  ||  s5.indexOf(ContentType)  ==  0  &&  outLinebyte[0]  ==  45)
                                break;
                          fileoutputstream.write(Linebyte,  0,  ai[0]);
                          fileoutputstream.flush();
                    }
              
                    byte  byte0;
                    if(newline.length()  ==  1)
                          byte0  =  2;
                    else
                          byte0  =  1;
                    if(s5  !=  null  &&  outLinebyte[0]  !=  45  &&  ai1[0]  >  newline.length()  *  byte0)
                          fileoutputstream.write(outLinebyte,  0,  ai1[0]  -  newline.length()  *  byte0);
                    if(sContentType  !=  null  &&  Linebyte[0]  !=  45  &&  ai[0]  >  newline.length()  *  byte0)
                          fileoutputstream.write(Linebyte,  0,  ai[0]  -  newline.length()  *  byte0);
                    
                    iFileSize  =  ffile.length();
                    fileoutputstream.close();
              }
        }
        
        private  String  readLine(byte  Linebyte[],  int  ai[],  ServletInputStream  servletinputstream,  String  CharacterEncoding){
              try{
                    //readLine(byte[]  buffer,  int  offset,  int  length)
                    //Reads  a  line  from  the  POST  data.
                    ai[0]  =  servletinputstream.readLine(Linebyte,  0,  Linebyte.length);
                    if(ai[0]  ==  -1)
                          return  null;
              }catch(IOException  _ex){
                    return  null;
              }
              
              try{
                    if(CharacterEncoding  ==  null){
                          //用缺省的编码方式把给定的byte数组转换为字符串
                          //String(byte[]  bytes,  int  offset,  int  length)
                          return  new  String(Linebyte,  0,  ai[0]);
                    }else{
                          //用给定的编码方式把给定的byte数组转换为字符串
                          //String(byte[]  bytes,  int  offset,  int  length,  String  enc)
                          return  new  String(Linebyte,  0,  ai[0],  CharacterEncoding);
                    }
              }catch(Exception  _ex){
                    return  null;
              }
        }  
    /*  
    public  int  readLine(byte[]  buffer,  
    int  offset,  
    int  length)  throws  java.io.IOException  
    从POST来的数据中读一行  
    参数:  
    buffer  -  buffer  to  hold  the  line  data  
    offset  -  offset  into  the  buffer  to  start  
    length  -  maximum  number  of  bytes  to  read.  
    Returns:  
    number  of  bytes  read  or  -1  on  the  end  of  line.  
    */  
    }