荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: baoli (生命因理想而伟大), 信区: Java
标  题: 【转载】使用JAVA技术实现文件的上传
发信站: 荔园晨风BBS站 (Tue Apr 15 19:27:29 2003), 站内信件

作者:ztyzty

下面是一个用来上传下达的小程序,希望大家给予指教.

import java.io.*;
import java.util.*;
import java.text.*;
import javax.mail.*;
import javax.servlet.*;
import javax.activation.*;
import javax.servlet.http.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMultipart;

/**
*  @author   2002/7/10 zty
*
*<p>
*<p> 【使用例】
*<p>  try {
*<p>    Upload load=new Upload();
*<p>    load.setDir("i:\\ztytest\\");
*<p>    load.doUpload(req,res);
*<p>              //HttpServletRequest req
*<p>                //HttpServletResponse res
*<p>  }
*<p>  catch( Exception e ) {
*<p>    return;
*<p>  }
*/

public class Upload extends HttpServlet {
    public String strDir;

    public Upload()
    {
    }

public void doUpload(HttpServletRequest req,HttpServletResponse res)
        throws IOException,MessagingException
    {
      String boundary=req.getHeader("Content-Type");
      int pos=boundary.indexOf('=');
      boundary=boundary.substring(pos+1);
      boundary="--"+ boundary;
      ServletInputStream in=req.getInputStream() ;
      byte[] bytes=new byte[512];
      int state=0;
      ByteArrayOutputStream buffer=new ByteArrayOutputStream();
      String name=null;
      String value=null;
      String filename=null;
      String contentType=null;

      int i=in.readLine(bytes,0,512);

      while (-1!=i)
      {
        String st=new String(bytes,0,i);
        if (st.startsWith(boundary))
        {
            state=0;
            System.out.println(filename);
            if (null!=name)
            {
                if(buffer.size()>2)
                {
FileOutputStream outStream = new FileOutputStream(strDir+filename);
outStream.write(buffer.toByteArray(),0,buffer.toByteArray().length);
outStream.close();
                }
                name=null;
                value=null;
                filename=null;
                contentType=null;
                buffer=new ByteArrayOutputStream();
            }
        }//end of st.startsWith(boundary)
else if (st.startsWith("Content-Disposition:form-data")&&state==0)
        {
StringTokenizer tokenizer=new StringTokenizer(st,";=\"");
            while(tokenizer.hasMoreTokens())
            {
                String token=tokenizer.nextToken();
                if(token.trim().startsWith("name"))
                {
                    name=tokenizer.nextToken();
                    state=2;
                }
                else if(token.trim().startsWith("filename"))
                {
                    filename=tokenizer.nextToken();
StringTokenizer ftokenizer=new StringTokenizer(filename,"\\");
                    filename=ftokenizer.nextToken();
                    while(ftokenizer.hasMoreTokens())
                        filename=ftokenizer.nextToken();
                    state=1;
                    break;
                }
            }
        }//end state=0
        else if (st.startsWith("Content-Type") && state==1)
        {
            pos=st.indexOf(":");
            contentType=st.substring(pos+2,st.length()-2);

        }//end state=1
        else if (state==1)
            state=3;
        else if (st.equals("\r\n")&&state==2)
            state=4;
        else if (state==3)
            buffer.write(bytes,0,i);
        else if (state==4)
            value=value==null?st:value+st;

        i=in.readLine(bytes,0,512);
      }//end while

    }

    public void setDir(String strSavePath)
    {
        strDir =strSavePath;
    }

    class ByteDataSource implements DataSource
    {
        byte[] bytes;
        String contentType;
        String name;

ByteDataSource(byte[] bytes,String contentType,String name)
        {
            this.bytes=bytes;
            this.contentType=contentType;
            this.name=name;
        }

        public String getContentType()
        {
            return contentType;
        }

        public InputStream getInputStream()
        {
return new ByteArrayInputStream(bytes,0,bytes.length-2);
        }

        public String getName()
        {
            return name;
        }

        public OutputStream getOutputStream() throws IOException
        {
            throw new FileNotFoundException();
        }
    }//end of class
}

--
成就最牛的传说!做最牛的牛魔王!

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 61.144.235.41]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店