欢迎光临
我们一直在努力

用jsp编写文件上载-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

如果你曾用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);

}

如果有不明白的发e-mail:yymailbox@263.net.bye

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用jsp编写文件上载-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章