首页 > 编程 > JSP > 正文

用JSP编写文件上传的程序

2024-09-05 00:20:29
字体:
来源:转载
供稿:网友
如果你曾用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(/"//r//n//r//n/");

temp=temp.substring(start1+4);
start2=temp.indexof(/";//r//n/");
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);

}
如果有不明白的到论坛中发表意见

}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表