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

解决方案 »

  1.   

    你可以去下载smartupload。很好用,干什么费尽自己写代码呢?
      

  2.   

    <%
     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来更新到数据库了
      

  3.   

    我已经把jspsmartupload.jar放到Tomcat 4.1\shared\lib里
    然后有如下两个文件:
    upload.jsp<%@ page contentType="text/html; charset=gb2312" %>
    <html>
    <head>
    <title>文件上传</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head><body>
    <p>&nbsp;</p>
    <p align="center">上传文件选择</p>
    <FORM METHOD="POST" ACTION="do_upload.jsp"
    ENCTYPE="multipart/form-data">
    <input type="hidden" name="TEST" value="good">
      <table width="75%" border="1" align="center">
        <tr> 
          <td><div align="center">1、 
              <input type="FILE" name="FILE1" size="30">
            </div></td>
        </tr>
        <tr> 
          <td><div align="center">2、 
              <input type="FILE" name="FILE2" size="30">
            </div></td>
        </tr>
        <tr> 
          <td><div align="center">3、 
              <input type="FILE" name="FILE3" size="30">
            </div></td>
        </tr>
        <tr> 
          <td><div align="center">4、 
              <input type="FILE" name="FILE4" size="30">
            </div></td>
        </tr>
        <tr> 
          <td><div align="center">
              <input type="submit" name="Submit" value="上传它!">
            </div></td>
        </tr>
      </table>
    </FORM>
    </body>
    </html>do_upload.jsp<%@ page contentType="text/html; charset=gb2312" language="java" 
    import="java.util.*,com.jspsmart.upload.*" errorPage="" %>
    <html>
    <head>
    <title>文件上传处理页面</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head><body>
    <%
    // 新建一个SmartUpload对象
    SmartUpload su = new SmartUpload();
    // 上传初始化
    su.initialize(pageContext);
    // 设定上传限制
    // 1.限制每个上传文件的最大长度。
    // su.setMaxFileSize(10000);
    // 2.限制总上传数据的长度。
    // su.setTotalMaxFileSize(20000);
    // 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
    // su.setAllowedFilesList("doc,txt");
    // 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
    jsp,htm,html扩展名的文件和没有扩展名的文件。
    // su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
    // 上传文件
    su.upload();
    // 将上传文件全部保存到指定目录
    int count = su.save("/upload");
    out.println(count+"个文件上传成功!<br>");

    // 利用Request对象获取参数之值
    out.println("TEST="+su.getRequest().getParameter("TEST")
    +"<BR><BR>"); // 逐一提取上传文件信息,同时可保存文件。
    for (int i=0;i<su.getFiles().getCount();i++)
    {
    com.jspsmart.upload.File file = su.getFiles().getFile(i);

    // 若文件不存在则继续
    if (file.isMissing()) continue; // 显示当前文件信息
    out.println("<TABLE BORDER=1>");
    out.println("<TR><TD>表单项名(FieldName)</TD><TD>"
    + file.getFieldName() + "</TD></TR>");
    out.println("<TR><TD>文件长度(Size)</TD><TD>" + 
    file.getSize() + "</TD></TR>");
    out.println("<TR><TD>文件名(FileName)</TD><TD>" 
    + file.getFileName() + "</TD></TR>");
    out.println("<TR><TD>文件扩展名(FileExt)</TD><TD>" 
    + file.getFileExt() + "</TD></TR>");
    out.println("<TR><TD>文件全名(FilePathName)</TD><TD>"
    + file.getFilePathName() + "</TD></TR>");
    out.println("</TABLE><BR>"); // 将文件另存
    // file.saveAs("/upload/" + myFile.getFileName());
    // 另存到以WEB应用程序的根目录为文件根目录的目录下
    // file.saveAs("/upload/" + myFile.getFileName(), 
    su.SAVE_VIRTUAL);
    // 另存到操作系统的根目录为文件根目录的目录下
    // file.saveAs("c:\\temp\\" + myFile.getFileName(), 
    su.SAVE_PHYSICAL); }
    %>
    </body>
    </html>当我上传图片的时候提示错误:
    org.apache.jasper.JasperException: Unable to compile class for JSPAn error occurred at line: 9 in the jsp file: /test/do_upload.jspGenerated servlet error:
        [javac] Compiling 1 source fileC:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:64: not a statement
    jsp,htm,html?╁??????欢??病???灞?????浠躲??
            ^An error occurred at line: 9 in the jsp file: /test/do_upload.jspGenerated servlet error:
    C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:64: ';' expected
    jsp,htm,html?╁??????欢??病???灞?????浠躲??
               ^An error occurred at line: 9 in the jsp file: /test/do_upload.jspGenerated servlet error:
    C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:64: illegal character: \12290
    jsp,htm,html?╁??????欢??病???灞?????浠躲??
                                       ^An error occurred at line: 9 in the jsp file: /test/do_upload.jspGenerated servlet error:
    C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:102: not a statement
    su.SAVE_VIRTUAL);
                      ^An error occurred at line: 9 in the jsp file: /test/do_upload.jspGenerated servlet error:
    C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:102: ';' expected
    su.SAVE_VIRTUAL);
                                   ^An error occurred at line: 9 in the jsp file: /test/do_upload.jspGenerated servlet error:
    C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:105: not a statement
    su.SAVE_PHYSICAL);
                      ^An error occurred at line: 9 in the jsp file: /test/do_upload.jspGenerated servlet error:
    C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\class\test\do_upload_jsp.java:105: ';' expected
    su.SAVE_PHYSICAL);
                                    ^
    7 errors
      

  4.   

    先前我搜索了关于该问题的疑答,好象效果还不是很满意的。
    我要求的功能是:
    1、可以能判断要上传的图片格式,一般只判断JPG、GIF两种格式,其它的图片格式禁止上传;
    2、限制图片的大小,一般超过200KB的图片禁止上传。
    3、后台的数据库用的是mysql,不知道怎样存贮图片?是把图片上传至服务器上的指定的目录下,再由数据库存贮该图片的路径名?还是直接把图片存贮到数据库的字段内?以上问题请各位大侠指点,能有代码指点最好,谢谢。
      

  5.   

    还是用 smartupload 好一些。
      

  6.   

    http://www.eastkeyi.com/help.rar你的问题这个里面就有,去下载看代码