用smartupload
http://www.jspsmart.com/

解决方案 »

  1.   

    上载:
    public void uploadFile(javax.servlet.http.HttpServletRequest req) throws IOException,Exception {
         try {
            String contentType=req.getContentType();
            int contentLength=req.getContentLength();
            java.io.DataInputStream bis=new java.io.DataInputStream(req.getInputStream());
            int once = 0;
            int total = 0;
            byte[] buffer=new byte[contentLength];
            while ((total<contentLength) && (once>=0)) {
              once = bis.read(buffer,total,contentLength);
              total += once;
            }
            int boundaryStart=contentType.indexOf("boundary=");
            boundaryStart=boundaryStart+"boundary=".length();
            String boundary="--"+contentType.substring(boundaryStart);
            int pos=getFormNameIndex(buffer,"filename=\"".getBytes(),1)+"filename=\"".length();
            int posEnd=getFormNameIndex(buffer,"\"".getBytes(),pos);
            String filename=new String(buffer,pos,posEnd-pos);
            pos=filename.lastIndexOf(".");
            String filenames=System.currentTimeMillis()/1000+filename.substring(pos);
            pos=getFormNameIndex(buffer,"Content-Type: ".getBytes(),1);
            pos=getFormNameIndex(buffer,"\r\n".getBytes(),pos+1)+4;
            int endpos=getFormNameIndex(buffer,boundary.getBytes(),pos+20);
            int len=endpos-pos;
            java.io.DataOutputStream bos= new java.io.DataOutputStream(new java.io.FileOutputStream(new java.io.File(filepath,filenames)));
            bos.write(buffer,pos,len);
            bis.close();
            bos.close();
         }catch(Exception exc) {
            throw new Exception("upload failed.");
         }finally{     }
      }
      private int getFormNameIndex(byte[] source,byte[] formname,int start) {
          int soulen=source.length;
          int sealen=formname.length;
          boolean hasSearch=false;
          int pos=-1;
          for(int i=start;i<soulen;i++) {
            if(source[i]==formname[0]) {
              boolean hasSear=true;
              for(int k=1;k<sealen;k++) {
                  if(source[i+k]!=formname[k]) {
                     hasSear=false;
                     break;
                   }
              }
              hasSearch=hasSear;
            }
            if(hasSearch) {
              pos=i;
              break;
            }
          }
         return pos;
      }
    JSP页面:
    <%@ page contentType="text/html; charset=GBK" %>
    <html>
    <head>
    <title>
    fileUpLoad
    </title>
    </head>
    <body>
    <form name=fileload method=post action="fileUpLoadOp.jsp">
    文件名:<input type="file" name="filename"><br>
    文件夹:<input type="checkbox" name="uploadfolder" value="1">
    <input type="submit" name="submit" value="上载">
    </form>
    </body>
    </html>
    JSP处理页面:
    <jsp:useBean id="fload" scope="page" class="learn.load.fileLoad" />
    <%
      fload.uploadFile(request);
    %>
      

  2.   

    那就参考一下这个:
    §http://www-900.ibm.com/developerWorks/java/fileup/index.shtml