List.jsp
<%@ page contentType="text/html; charset=gb2312"%>
<%@ include file="/DataIni/DataOpen.jsp"%>
<%@ include file="/ScriptLib/Init.jsp"%>
<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">
<script>
function MediaSubmit(TypeValue,Id)
{
MediaForm.TypeValue.value=TypeValue;
MediaForm.RecordId.value=Id;
MediaForm.submit();
}
function ShowMedia(Id,Type)
{
var hwnd=window.open('','','');
switch(Type)
{
case "image/*":
hwnd.document.write("<Img src='/System/Media/Show.jsp?Id="+Id+"&Type="+Type+"' border=0></a>");
break;
case "audio/*":
hwnd.document.write("<BgSound src='/System/Media/Show.jsp?Id="+Id+"&Type="+Type+"' border=0>");
break;
case "video/*":
hwnd.document.write("<Img dynsrc='/System/Media/Show.jsp?Id="+Id+"&Type="+Type+"' border=0></a>");
break;
case "application/msword":
hwnd.location.href="/System/Media/Show.jsp?Id="+Id+"&Type="+Type;
break;
case "application/vnd.ms-excel":
hwnd.location.href="/System/Media/Show.jsp?Id="+Id+"&Type="+Type;
break;
case "application/*":
hwnd.location.href="/System/Media/Show.jsp?Id="+Id+"&Type="+Type;
break;
default:
break;
}
}
</script>
</head><body>
<%@ include file="/TemplateLib/Head.jsp"%>
<center>
<form action="Result.jsp" method="post" name="MediaForm" enctype="multipart/form-data">
<input type="hidden" name="TypeValue" size="10" value="">
<input type="hidden" name="RecordId" size="10" value="">
<table border="0" cellpadding="0" cellspacing="0" width="80%">
  <tr>
    <td width="10%" class="TdClass_Head">多媒体库</td>
  </tr>
</table>
<br>
<table border="0" width="80%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="10%" class="TdClass_0">媒体描述</td>
    <td width="30%" class="TdClass_0">媒体分类</td>
    <td width="10%" class="TdClass_0">媒体类型</td>
    <td width="40%" class="TdClass_0">媒体文件</td>
    <td width="10%" class="TdClass_0">执行操作</td>
  </tr>
  <tr>    
    <td width="10%" class="TdClass_1"><input name="Re" size="10" value=""></td>
    <td width="30%" class="TdClass_1">
      <select name="Category" size=1 style="width:80%">
<option value="">--请选择--</option>
<% 
odbcQuery="select distinct Category from MediaLib";
odbcRs=odbcStmt.executeQuery(odbcQuery);
String Category;
while(odbcRs.next())
{
Category=odbcRs.getString("Category");
%>                
        <option value="<%=Category%>"><%=Category%></option>
<%
}
odbcRs.close();
%>
      </select>
<script>
function AddCategory()
{
var m_Category="";
m_Category=window.prompt("请输入媒体分类","");
if(m_Category!="")
{
MediaForm.Category.length=MediaForm.Category.length+1;
MediaForm.Category.options[MediaForm.Category.length-1].text=m_Category;
MediaForm.Category.options[MediaForm.Category.length-1].value=m_Category;
MediaForm.Category.options[MediaForm.Category.length-1].selected="selected";
}
}
</script>      
      <input type="button" value="." id="" name="" onclick="AddCategory()">      
    </td>
    <td width="20%" class="TdClass_1">
      <select name="Type" size=1 style="width:80%">
<option value="">--请选择--</option>
<option value="image/*">图像</option>
<option value="audio/*">音频</option>
<option value="video/*">视频</option>
<option value="application/msword">文档</option>
<option value="application/vnd.ms-excel">表格</option>
<option value="application/*">应用</option>
  </select>
</td>
    <td width="30%" class="TdClass_1"><input type="file" name="MediaFile" size="10" value=""><a href="#">浏览媒体</a></td>
    <td width="10%" class="TdClass_1"><input type="button" onclick="MediaSubmit('Add','NULL')" value="增加" id=Add name=Add></td>
  </tr>
<%
odbcQuery="select * from MediaLib";
odbcRs_=odbcStmt.executeQuery(odbcQuery);
String Id,Type;
int iCount;
iCount=0;
while(odbcRs_.next())
{
Id=odbcRs_.getString("ID");
Type=odbcRs_.getString("Type");
if(iCount%2==0)
{
%>
  <tr>
    <td width="10%" class="TdClass_0"><input name="Re<%=Id%>" size="10" value="<%=odbcRs_.getString("Re")%>"></td>
    <td width="30%" class="TdClass_0"><input name="Category<%=Id%>" size="10" value="<%=odbcRs_.getString("Category")%>" readonly></td>
    <td width="10%" class="TdClass_0"><input name="Type<%=Id%>" size="10" value="<%=Type%>" readonly></td>
    <td width="40%" class="TdClass_0"><input type="file" name="MediaFile<%=Id%>" size="10" value=""><a href="#" onclick="ShowMedia('<%=Id%>','<%=Type%>')">浏览媒体</a></td>
    <td width="10%" class="TdClass_0"><input type="button" onclick="MediaSubmit('Modify','<%=Id%>')" value="修改" id=Modify name=Modify><input type="button" onclick="MediaSubmit('Del','<%=Id%>')" value="删除" id=Del name=Del></td>
  </tr>
<% }else{%>
  <tr>
    <td width="10%" class="TdClass_1"><input name="Re<%=Id%>" size="10" value="<%=odbcRs_.getString("Re")%>"></td>
    <td width="30%" class="TdClass_1"><input name="Category<%=Id%>" size="10" value="<%=odbcRs_.getString("Category")%>" readonly></td>    
    <td width="10%" class="TdClass_1"><input name="Type<%=Id%>" size="10" value="<%=Type%>" readonly></td>
    <td width="40%" class="TdClass_1"><input type="file" name="MediaFile<%=Id%>" size="10" value=""><a href="#" onclick="ShowMedia('<%=Id%>','<%=Type%>')">浏览媒体</a></td>
    <td width="10%" class="TdClass_1"><input type="button" onclick="MediaSubmit('Modify','<%=Id%>')" value="修改" id=Modify name=Modify><input type="button" onclick="MediaSubmit('Del','<%=Id%>')" value="删除" id=Del name=Del></td>
  </tr>
<%
}
iCount++;
}
odbcRs_.close();
%>        
</table>
<table border="0" cellpadding="0" cellspacing="0" width="80%">
  <tr>
    <td width="100%" class="TdClass_Head"><input type="button" value="返回" id="Return" name="Return" onclick="window.location.href='/System/Index.jsp'"></td>
  </tr>
</table>
</form>
</center>
<%@ include file="/TemplateLib/Tail.jsp"%>
<%@ include file="/DataIni/DataClose.jsp"%>
</body></html>

解决方案 »

  1.   

    Result.jsp
    <%@ page contentType="text/html; charset=gb2312"%>
    <%@ include file="/DataIni/DataOpen.jsp"%>
    <%@ include file="/ScriptLib/Init.jsp"%>
    <%
    InputStream InData=null;
    int iSize,iLength;
    iLength=request.getContentLength();
    byte[] Buffer=new byte[iLength];
    byte[] Byte_File;
    if(iLength>0)
    {
    InData=request.getInputStream();
    String ContentType=request.getContentType();
    iSize=InData.read(Buffer);
    if(iSize!=-1)
    {
    //数据处理开始
    String FileData=new String(Buffer);

    //正则字串:"name=\"";
    //正则字串:"filename=\"";

    String strSplit=ContentType.substring(ContentType.lastIndexOf("=")+1,ContentType.length());
    String strSub[]=FileData.split(strSplit); String strElement;
    int iBegin,iEnd;
    iBegin=0;iEnd=0;
    String FieldName,FieldValue;
    int bTrue;
    bTrue=0;
    int iPos;
    iPos=0;
    int iLocation;
    iLocation=0;
    for(int iIndex=0;iIndex<strSub.length;iIndex++)
    {
    strElement=strSub[iIndex];
    iBegin=strElement.indexOf("name=\"",0);
    if(iBegin!=-1)
    {
    iEnd=strElement.indexOf("\"",iBegin+6);
    FieldName=strElement.substring(iBegin+6,iEnd);
    iBegin=strElement.indexOf("filename=\"",0);
    if(iBegin!=-1)
    {
    bTrue=1;
    }
    //
    iEnd=strElement.indexOf("\r\n\r\n",0);
    if(bTrue==1)
    {
    iLocation=0;
    iPos=FileData.indexOf("filename=\"",iPos);
    iPos=FileData.indexOf("\n",iPos)+1;
    iPos=FileData.indexOf("\n",iPos)+1;
    iPos=FileData.indexOf("\n",iPos)+1;
    iLocation=FileData.indexOf(strSplit,iPos)-4;
    FieldValue=FileData.substring(iPos,iLocation);
    byte[] Byte_File_=FieldValue.getBytes();
    Byte_File=new byte[Byte_File_.length];
    for(int kIndex=0;kIndex<Byte_File_.length;kIndex++)
    {
    Byte_File[kIndex]=Buffer[iPos+0+kIndex];
    //0:Excel
    //0:Word
    //
    }
    session.putValue(FieldName,Byte_File);
    }
    else
    {
    FieldValue=strElement.substring(iEnd+4);
    session.putValue(FieldName,FieldValue.substring(0,FieldValue.indexOf("\n")-1));
    }
    bTrue=0;
    }
    }
    //数据处理结束

    String TypeValue,Id;
    TypeValue=(String)session.getValue("TypeValue");
    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");
    boolean bDefault;
    int iC;
    oracle.sql.BLOB m_Blob;
    switch(iDoAction)
    {
    case 1:
    bDefault=odbcConn.getAutoCommit();
    odbcConn.setAutoCommit(false);
    odbcStmt.executeUpdate("insert into MediaLib(ID,Category,Type,MediaFile,Re) values(IDSERIAL.nextval,'"+(String)session.getValue("Category")+"','"+(String)session.getValue("Type")+"',EMPTY_BLOB(),'"+(String)session.getValue("Re")+"')");
    odbcQuery="select * from MediaLib order by ID asc";
    odbcRs=odbcStmt___.executeQuery(odbcQuery);
    odbcRs.last();
    String iID=String.valueOf((int)odbcRs.getInt("ID"));
    odbcQuery="select MediaFile from MediaLib where ID="+iID+" for Update";
    odbcRs=odbcStmt___.executeQuery(odbcQuery);
    odbcRs.last();
    m_Blob=(oracle.sql.BLOB)odbcRs.getBlob("MediaFile");
    BufferedOutputStream pOutAdd=new BufferedOutputStream(m_Blob.getBinaryOutputStream());
    ByteArrayInputStream pInAdd=new ByteArrayInputStream((byte[])session.getValue("MediaFile"));
    while((iC=pInAdd.read())!=-1)
    {pOutAdd.write(iC);}
    pInAdd.close();
    pOutAdd.close();
    odbcConn.commit();
    odbcConn.setAutoCommit(bDefault);
    response.sendRedirect("/System/Media/List.jsp");
    break;
    case 2:
    bDefault=odbcConn.getAutoCommit();
    odbcConn.setAutoCommit(false);
    odbcStmt.executeUpdate("update MediaLib set MediaFile=EMPTY_BLOB() where ID="+Id);
    odbcRs=odbcStmt___.executeQuery(odbcQuery);
    odbcRs.last();
    m_Blob=(oracle.sql.BLOB)odbcRs.getBlob("MediaFile");
    BufferedOutputStream pOutModify=new BufferedOutputStream(m_Blob.getBinaryOutputStream());
    ByteArrayInputStream pInModify=new ByteArrayInputStream((byte[])session.getValue("MediaFile"));
    while((iC=pInModify.read())!=-1)
    {pOutModify.write(iC);}
    pInModify.close();
    pOutModify.close();
    odbcConn.commit();
    odbcConn.setAutoCommit(bDefault);
    response.sendRedirect("/System/Media/List.jsp");
    break;
    case 3:
    odbcStmt.executeUpdate("delete from MediaLib where ID="+Id);
    response.sendRedirect("/System/Media/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/Media/List.jsp';">
    <%@ include file="/TemplateLib/Head.jsp"%>
    <%@ include file="/TemplateLib/Tail.jsp"%>
    <%@ include file="/DataIni/DataClose.jsp"%>
    </body></html>
      

  2.   

    Show.jsp
    <%@ 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.   

    利用RFC1867传输二进制文件流由服务器处理并放到数据库[一般是二进制字段或者BLOB字段或者OLE字段分别对应SQL Server,Oracle,Access]例子是Oracle的.