用JSP编写文件上传
程序员文章站
2023-12-17 11:52:40
如果你曾用vb编写文件上传的组件的话,那么用java编写文件上传的javabean十分容易。 下面的例子只是一个简版 package yuanyifileup; impor...
如果你曾用vb编写文件上传的组件的话,那么用java编写文件上传的javabean十分容易。
下面的例子只是一个简版
package yuanyifileup;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.pagecontext;
public class yuanyifileup
{
private servletrequest request;
private servletresponse response;
private servletconfig config;
servletinputstream data;
int formsize;
file f1;
fileoutputstream os;
datainputstream is;
string filename;
byte[] b;
byte t;
boolean flag=false;
public yuanyifileup()
{ }
public void initialize(servletconfig config,httpservletrequest request,httpservletresponse response) throws ioexception
{
this.request=request;
this.response=response;
this.config=config;
data = request.getinputstream();
formsize=request.getcontentlength();
}
public void initialize(pagecontext pagecontext) throws ioexception
{
request=pagecontext.getrequest();
response=pagecontext.getresponse();
config=pagecontext.getservletconfig();
data = request.getinputstream();
formsize=request.getcontentlength();
}
public boolean setfilename(string s)
{
try
{
file f1=new file(s);
os=new fileoutputstream(f1);
}
catch(ioexception e)
{return(false);}
return(true);
}
public void getbyte()
{
int i=0;
try
{
is=new datainputstream(data);
b=new byte[formsize];
while (true)
{
try
{
t=is.readbyte();
b[i]=t;
i++;
}
catch(eofexception e)
{ break;}
}
is.close();}
catch(ioexception e)
{}
}
public boolean save()
{
int i=0,start1=0,start2=0;
string temp="";
if (!flag)
{
getbyte();
flag=true;
}
try
{
temp=new string(b,"iso8859_1");
}
catch(unsupportedencodingexception e)
{return(false);}
start1=temp.indexof("image/");
temp=temp.substring(start1);
start1=temp.indexof("rnrn");
temp=temp.substring(start1+4);
start2=temp.indexof(";rn");
if (start2!=-1)
{
temp=temp.substring(0,start2);
}
try
{
byte[] img=temp.getbytes("iso8859_1");
for (i=0;i<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(ioexception e)
{return(false);}
return(true);
}
如果有不明白的发e-mail:yymailbox@263.net.bye
}
下面的例子只是一个简版
package yuanyifileup;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.pagecontext;
public class yuanyifileup
{
private servletrequest request;
private servletresponse response;
private servletconfig config;
servletinputstream data;
int formsize;
file f1;
fileoutputstream os;
datainputstream is;
string filename;
byte[] b;
byte t;
boolean flag=false;
public yuanyifileup()
{ }
public void initialize(servletconfig config,httpservletrequest request,httpservletresponse response) throws ioexception
{
this.request=request;
this.response=response;
this.config=config;
data = request.getinputstream();
formsize=request.getcontentlength();
}
public void initialize(pagecontext pagecontext) throws ioexception
{
request=pagecontext.getrequest();
response=pagecontext.getresponse();
config=pagecontext.getservletconfig();
data = request.getinputstream();
formsize=request.getcontentlength();
}
public boolean setfilename(string s)
{
try
{
file f1=new file(s);
os=new fileoutputstream(f1);
}
catch(ioexception e)
{return(false);}
return(true);
}
public void getbyte()
{
int i=0;
try
{
is=new datainputstream(data);
b=new byte[formsize];
while (true)
{
try
{
t=is.readbyte();
b[i]=t;
i++;
}
catch(eofexception e)
{ break;}
}
is.close();}
catch(ioexception e)
{}
}
public boolean save()
{
int i=0,start1=0,start2=0;
string temp="";
if (!flag)
{
getbyte();
flag=true;
}
try
{
temp=new string(b,"iso8859_1");
}
catch(unsupportedencodingexception e)
{return(false);}
start1=temp.indexof("image/");
temp=temp.substring(start1);
start1=temp.indexof("rnrn");
temp=temp.substring(start1+4);
start2=temp.indexof(";rn");
if (start2!=-1)
{
temp=temp.substring(0,start2);
}
try
{
byte[] img=temp.getbytes("iso8859_1");
for (i=0;i<img.length;i++)
{ os.write(img[i]); }
os.close();
}
catch(ioexception e)
{return(false);}
return(true);
}
如果有不明白的发e-mail:yymailbox@263.net.bye
}