下面是一个用来上传下达的小程序,希望大家给予指教.
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
*
*<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
}
闽公网安备 35060202000074号