还是SmartUpload,嘿嘿………:)你为什么要找另一个呢?有什么问题?而且你拭过自己写一个吗?网上也有这方面的资料,一个很流行的上传文件的bean代码,你手上有吗?

解决方案 »

  1.   

    Jason Hunter的com.oreilly.servlet(cos)包我想不会比SmartUpload差吧?http://www.servlets.com/cos/index.html
      

  2.   

    有问题啊!!SmartUpload与我们的盟友apusic有冲突啊!
      

  3.   


    哦,这样就没办法了。你一直是用apusic吗?这个好用吗,能否介绍一下。不是他们自己介绍的广告语,是你使用的心得。多谢.
      

  4.   

    敝人写的JPowerSmartUpload,要下载在hicharlie.yeah.net
      

  5.   

    apusic里就有SmartUpload的demo怎么会有问题
      

  6.   

    package com.codetree.util;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.ServletInputStream;
    import java.util.Dictionary;
    import java.util.Hashtable;
    import java.io.PrintWriter;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;public class FileUploadBean{
      private String savePath, filepath, filename, contentType;
      private Dictionary fields;
      //Begin Rayman's modification
      private static final int BUF_SIZE = 256;
      //End Rayman's modification  public String getFilename() {
        return filename;
      }  public String getFilepath() {
        return filepath;
      }  public void setSavePath(String savePath) {
        this.savePath = savePath;
      }  public String getContentType() {
        return contentType;
      }  public String getFieldValue(String fieldName) {
        if (fields == null || fieldName == null)
        {
          //System.out.println(fieldName);
          //System.out.println("if loop");
          return null;
        }
        //System.out.println(fieldName);
        //System.out.println(fields.get("company"));
        return (String)fields.get(fieldName);
      }  private void setFilename(String s) {    if (s==null)
          return;    int pos = s.indexOf("filename=\"");
        if (pos != -1) {
          filepath = s.substring(pos+10, s.length()-1);
          // Windows browsers include the full path on the client
          // But Linux/Unix and Mac browsers only send the filename
          // test if this is from a Windows browser
          pos = filepath.lastIndexOf("\\");
          if (pos != -1)
            filename = filepath.substring(pos + 1);
          else
            filename = filepath;
        }
      }  private void setContentType(String s){
        if (s==null)
          return;    int pos = s.indexOf(": ");
        if (pos != -1)
          contentType = s.substring(pos+2, s.length());
      }  public void doUpload(HttpServletRequest request) throws IOException{    ServletInputStream in = request.getInputStream();
        byte[] line = new byte[BUF_SIZE];
        int i = in.readLine(line, 0, 128);
        if (i < 3)
          return;
        int boundaryLength = i - 2;    String boundary = new String(line, 0, boundaryLength); //-2 discards the newline character
        fields = new Hashtable();    while (i != -1) {
          String newLine = new String(line, 0, i);
          if (newLine.startsWith("Content-Disposition: form-data; name=\"")) {
            if (newLine.indexOf("filename=\"") != -1) {
              setFilename(new String(line, 0, i-2));
              if (filename==null)
                return;
              //this is the file content
              i = in.readLine(line, 0, 128);
              setContentType(new String(line, 0, i-2));
              i = in.readLine(line, 0, 128);
              
              //Begin Rayman's modification
                FileOutputStream pw = new FileOutputStream((savePath==null? "" : savePath) + filename);
                boolean done = false;
                int k = 0;
                byte c1 = 0x0d;
                byte c2 = 0x0a;            byte [] boundarybytes = boundary.getBytes();
                byte [] magickey = new byte[boundarybytes.length + 2];
                magickey[0] = c1;
                magickey[1] = c2;
                for(int t = 0;t<boundarybytes.length;t++)
                {
                    magickey[2+t] = boundarybytes[t];
                }
                for(int j = 0;j<magickey.length;j++)
                {
                    System.out.print((int)magickey[j]);
                }
                System.out.println("magickey==" + new String(magickey));
                System.out.println("magickey's length ==" + magickey.length);
                byte [] buf = new byte[magickey.length];
                int count = 0;
                byte c = -1;
                boolean pause = false;
              while (true) {
                // the problem is the last line of the file content
                // contains the new line character.
                // So, we need to check if the current line is
                // the last line
                if(!pause)
                {
                    byte [] one = new byte[1];
                    if(in.read(one)==-1) break;
                     c = one[0];
                    count ++;
                }
                else
                {
                    pause = false;
                }            if(c==magickey[k])
                {
                    System.out.print((int)c + " " + (char)c + " ");
                    if(k == magickey.length-1)
                    {
                        //It's last line.
                        System.out.println("Last line!" + " count==" + count);
                        break;
                    }
                    buf[k] = (byte)c;
                    k++;
                }
                else
                {
                    if(k>0)
                    {                    pw.write(buf,0,k);                }
                    if(c==magickey[0])
                    {
                          pause = true;
                    }
                    else
                    {
                        pw.write(c);
                    }
                    k=0;
                }
              }
              pw.close();
              //End Rayman's modification        }
            else {
              //this is a field
              // get the field name
              int pos = newLine.indexOf("name=\"");
              String fieldName = newLine.substring(pos+6, newLine.length()-3);
              //System.out.println("fieldName:" + fieldName);
              // blank line
              i = in.readLine(line, 0, 128);
              i = in.readLine(line, 0, 128);
              newLine = new String(line, 0, i);
              StringBuffer fieldValue = new StringBuffer(128);
              while (i != -1 && !newLine.startsWith(boundary)) {
                // The last line of the field
                // contains the new line character.
                // So, we need to check if the current line is
                // the last line.
                i = in.readLine(line, 0, 128);
                if ((i==boundaryLength+2 || i==boundaryLength+4) // + 4 is eof
                  && (new String(line, 0, i).startsWith(boundary)))
                  fieldValue.append(newLine.substring(0, newLine.length()-2));
                else
                  fieldValue.append(newLine);
                newLine = new String(line, 0, i);
              }
              //System.out.println("fieldValue:" + fieldValue.toString());
              fields.put(fieldName, fieldValue.toString());
            }
          }
          i = in.readLine(line, 0, 128);    } // end while
      }  public FileUploadBean()
      {
      }
      public static void main(String [] args)
      {
          /*  try
            {
                new FileUploadBean().raymanPseudoUpload();
            }catch(Exception e)
            {
                e.printStackTrace();
            }*/
      }}
      

  7.   

    The original article is here
    http://www.onjava.com/pub/a/onjava/2001/04/05/upload.html
    However ,it seems the foreign expert's code doesn't work when a non-plain text file is uploaded,
    so I made a small modification on it.
    Now it can support both plain text and binary file,
    hopefully it 's helpful.
      

  8.   

    SmartUpload
    上面的也不错的
      

  9.   

    http://www.servlets.com/cos/index.html
    is good thing!!!
    Orangehk.com is using too!!!