你用multipart/form-data时就不可以再用request.getParameter

解决方案 »

  1.   

    这个例子是分析multipart/form-data按RFC1867文档的.注意我把值全部放到Session了,当然在你操作的时候可以放到别的地方,如果不想这么做,可以下载组件jspsmart
    <%@ page contentType="text/html; charset=gb2312"%>
    <%@ include file="/DataIni/DataOpen.jsp"%>
    <%@ include file="/ScriptLib/Init.jsp"%>
    <%
    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_ColligateStatistic\\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 TypeValue,Id,ItemID;
    TypeValue=(String)session.getValue("TypeValue");
    ItemID=(String)session.getValue("ItemId");
    %><br><%=TypeValue%><br><%=ItemID%><br><%
    if(TypeValue!=null)
    {
    int iDoAction;
    iDoAction=0;
    if(TypeValue.indexOf("Add",0)!=-1)
    {iDoAction=1;}
    if(TypeValue.indexOf("Modify")!=-1)
    {iDoAction=2;}
    if(TypeValue.indexOf("Del")!=-1)
    {iDoAction=3;}
    odbcQuery="";
    Id=(String)session.getValue("RecordId");
    switch(iDoAction)
    {
    case 1:
    odbcStmt.executeUpdate("insert into GeneralSystem(ItemID,Category,Subject,Body,History,Type) values('"+ItemID+"','"+(String)session.getValue("Category")+"','"+(String)session.getValue("Subject")+"','"+(String)session.getValue("Body")+"',Now(),'"+(String)session.getValue("OLEFile_FileType")+"')");
    odbcRs=odbcStmtUpdate.executeQuery("select top 1 * from GeneralSystem order by ID desc");
    if(odbcRs.next())
    {
    byte[] OLEFile=(byte[])session.getValue("OLEFile");
    odbcRs.updateBinaryStream("OLEFile",new ByteArrayInputStream(OLEFile),OLEFile.length);
    odbcRs.updateRow();
    odbcRs.close();
    response.sendRedirect("/System/GeneralSystem/List.jsp");
    }
    break;
    case 2:
    response.sendRedirect("/System/GeneralSystem/List.jsp");
    break;
    case 3:
    odbcStmt.executeUpdate("delete from GeneralSystem where ID="+Id);
    response.sendRedirect("/System/GeneralSystem/List.jsp");
    break;
    }
    }
    }
    %>
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title></title>
    <link rel="stylesheet" type="text/css" href="/StyleLib/Default_Style_Sheet.css">
    </head><body onload="window.alert('动作失败,系统取消操作!');window.location.href='/System/GeneralSystem/List.jsp';">
    <%@ include file="/TemplateLib/Head.jsp"%>
    <%@ include file="/TemplateLib/Tail.jsp"%>
    <%@ include file="/DataIni/DataClose.jsp"%>
    </body></html>
      

  2.   

    save.jsp??你是用jsp?
    我不会,呵呵
    不好意思
      

  3.   

    你要分成开成为两个表单.然后分别提交<form method="post" action="save.jsp" name="info" ENCTYPE="multipart/form-data" onsubmit="document.info2.submit();">
    <input type="file" name="file1" onchange="document.info2.showcontent.value=this.value">
    <input type="submit">
    </form><form name="info2" target="_blank">
    <input type="hidden" name="showcontent" value="111">
    </form>